-
Notifications
You must be signed in to change notification settings - Fork 3
/
IPL Analysis in SQL.sql
199 lines (161 loc) · 6.43 KB
/
IPL Analysis in SQL.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Visit https://colearninglounge.com/blogs/ for more indepth and hands-on learning
// Download the dataset - https://www.kaggle.com/datasets/colearninglounge/ipl-dataset-download-python-tableau-analysis
// Adding a comment
SELECT *
FROM yog_ai.deliveries_3
LIMIT 5 ;
SELECT match_id, team1 as home, team2 as away, winner
FROM yog_ai.matches_3
WHERE season = '2022' ;
-- Display the final match
SELECT max(match_id)
FROM yog_ai.matches_3
WHERE season = '2022' and match_number is NULL; -- is NULL, ISNULL
SELECT match_id, team1 as home, team2 as away, winner, win_by, winner_type
FROM yog_ai.matches_3
WHERE season = '2022' and match_number is NULL
ORDER BY match_id desc
limit 1;
SELECT match_id, team1 as home, team2 as away, winner, win_by, winner_type
FROM yog_ai.matches_3
WHERE season = '2022' and match_number is NULL
ORDER BY start_date desc
LIMIT 1;
-- Player of the series
-- Find the player who have won "player of the match" highest number of times
-- 1. "player_of_match"
-- 2. Find the player with maximum occurance
SELECT max(player_of_match) as player_of_the_series
FROM yog_ai.matches_3
WHERE season = '2021' ;
-- Find the player of the match season wise
-- 1. season, player_of_match
-- 2. Group by season and count maximum player_of_match
SELECT season, max(player_of_match) as player_of_series
FROM yog_ai.matches_3
GROUP BY season
ORDER BY season;
-- Find out number of times player have been awarded player of the match
-- 1. player name <-- player_of_match
-- 2. number of times <-- count
-- 3. group by player_of_match
SELECT player_of_match, count(player_of_match) as no_of_pom
FROM yog_ai.matches_3
GROUP BY player_of_match
ORDER BY no_of_pom desc
SELECT season, player_of_match, count(player_of_match) as no_of_pom
FROM yog_ai.matches_3
WHERE match_number is not NULL
GROUP BY player_of_match, season
HAVING season = '2020/21'
ORDER BY season desc, no_of_pom
SELECT *
FROM yog_ai.matches_3 ;
-- Get the unique values of season
SELECT DISTINCT season
FROM yog_ai.deliveries
ORDER BY season;
SELECT DISTINCT season, match_id
FROM yog_ai.deliveries
ORDER BY season, match_id desc;
SELECT DISTINCT season, match_id
FROM yog_ai.deliveries
ORDER BY season desc, match_id;
SELECT DISTINCT season, match_id
FROM yog_ai.deliveries
ORDER BY season desc, match_id desc;
SELECT COUNT(*)
FROM yog_ai.venue_3
-- 1. filter 2022 data from matches table
-- 2. Group by winner to get total no. of wins for each team
-- 3. Order by based on wins
-- 4. Calculate the points
-- 5. Find the total matches - (10 + 18 + 8 + 14 + 12 + 8)/10 = 70/10 = 7 * 2 = 14
SELECT winner as team, (SELECT ((count(*)/count(DISTINCT winner)) * 2) as matches
FROM yog_ai.matches_3
WHERE season = '2022' and match_number is not NULL) as matches,
count(winner) as win,
(14 - count(winner)) as loss,
count(winner) * 2 as points
FROM yog_ai.matches_3
WHERE season = '2022' and match_number is not NULL
GROUP BY winner
ORDER BY win desc;
-- (10 + 18 + 8 + 14 + 12 + 8)/10 = 70/10 = 7 * 2 = 14
SELECT ((count(*)/count(DISTINCT winner)) * 2) as matches
FROM yog_ai.matches_3
WHERE season = '2022' and match_number is not NULL ;
SELECT *
FROM yog_ai.matches_3
WHERE match_number is not NULL and outcome is not NULL
SELECT * -- Total - 950. Unique - 950
FROM yog_ai.matches_3 ;
SELECT *
FROM yog_ai.venue_3 ;
SELECT * -- Total - 225954, Unique - 950
FROM yog_ai.deliveries ;
-- How many matches were lost by team batting first(won the toss) and scored more than 200 runs?
-- 1. match_id, batting_team(innings), runs(> 200), winner, toss_winner
-- 2. match_id, winner, toss_decision --> matches | innings, total_runs(runs_off_bat + extras) --> deliveries
-- 3. JOIN
-- 4. How many matches --> count, winner != batting_team, toss_decision == bat, total_runs > 200
SELECT m.match_id, max(m.winner), sum(d.runs_off_bat + d.extras) as total_runs -- d.innings, m.winner, m.toss_winner, m.toss_decision,
FROM yog_ai.deliveries d
JOIN yog_ai.matches_3 m
ON d.match_id = m.match_id
WHERE m.toss_decision = 'bat' and m.winner != m.toss_winner and innings = 1
GROUP BY m.match_id
HAVING sum(d.runs_off_bat + d.extras) > 200
-- d.innings, m.winner, m.toss_winner, m.toss_decision,
SELECT COUNT(*)
FROM (SELECT m.match_id, max(m.winner), sum(d.runs_off_bat + d.extras) as total_runs
FROM yog_ai.deliveries d
JOIN yog_ai.matches_3 m
ON d.match_id = m.match_id
WHERE m.toss_decision = 'bat' and m.winner != m.toss_winner and innings = 1
GROUP BY m.match_id) a
WHERE a.total_runs > 200
SELECT m.match_id, max(m.winner), sum(d.runs_off_bat + d.extras) as total_runs
SELECT max(sum(d.runs_off_bat + d.extras)) as higest_score
FROM yog_ai.deliveries d
JOIN yog_ai.matches_3 m
ON d.match_id = m.match_id
WHERE m.toss_decision = 'bat' and m.winner != m.toss_winner and innings = 1
GROUP BY m.match_id
ORDER BY total_runs desc
LIMIT 2
SELECT MAX(total_runs) as higest_score
FROM (SELECT sum(d.runs_off_bat + d.extras) as total_runs
FROM yog_ai.deliveries d
JOIN yog_ai.matches_3 m
ON d.match_id = m.match_id
WHERE m.toss_decision = 'bat' and m.winner != m.toss_winner and innings = 1
GROUP BY m.match_id) a
-- SELECT
-- FROM/JOIN
-- WHERE/HAVING
-- Example of subqeuery in WHERE. From here - https://learnsql.com/blog/sql-subquery-examples/
SELECT name, listed_price
FROM paintings
WHERE listed_price > (SELECT AVG(listed_price)
FROM paintings)
SELECT match_id,
innings,
team,
match_score,
match_score/CASE
WHEN over >= 19.6 OR wickets = 10 THEN 20
ELSE ((split_part(over::TEXT, '.', 1))::INT + 1.0/(split_part(over::TEXT, '.', 2)::INT))
END AS nrr
FROM (SELECT match_id,
innings,
Max(batting_team) as team,
Max(ball) AS over,
Sum(runs_off_bat + extras) AS match_score,
Count(player_dismissed) AS wickets
FROM yog_ai.deliveries
WHERE season = '2022'
GROUP BY match_id, innings
ORDER BY match_id, innings) a
-- What is the lowest first innings score of the tournament in ______?
-- How many runs team DC scored at Dubai in the matches they won?