-
Notifications
You must be signed in to change notification settings - Fork 0
/
Olympics_SQL_Queries.sql
377 lines (302 loc) · 11.9 KB
/
Olympics_SQL_Queries.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
-- 1. How many olympic games have been held?
USE Practice
SELECT COUNT( DISTINCT Games) AS total_games_held
FROM olympic_history
-- 2. List down all Olympic games held so far.
SELECT DISTINCT
Games,
City
FROM olympic_history
ORDER BY Games
-- 3. Mention the total no of nations who participated in each olympic games.
SELECT DISTINCT
Games,
City,
COUNT(DISTINCT NOC) AS number_of_nations_participating
FROM olympic_history
GROUP BY Games, City
ORDER BY Games
-- 4. Which year saw the highest and lowest no of countries participating in the olympics?
WITH
nations_participating AS (
SELECT DISTINCT
Games,
City,
COUNT(DISTINCT NOC) AS number_of_nations_participating
FROM olympic_history
GROUP BY Games, City
)
SELECT Games,
City,
number_of_nations_participating
FROM nations_participating
WHERE number_of_nations_participating IN (
(SELECT MIN(number_of_nations_participating) FROM nations_participating),
(SELECT MAX(number_of_nations_participating) FROM nations_participating)
)
-- 5. Which nation has participated in all of the olympic games?
-- How many olympics were held in total:
SELECT COUNT( DISTINCT Games) AS total_games_held
FROM olympic_history
-- Nations that participated in all of the olympics:
SELECT n.region,
COUNT(DISTINCT o.Games) AS total_games_participated
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY n.region
HAVING COUNT(DISTINCT Games) = 51
-- OR
WITH
olympic_games_held AS (
SELECT COUNT( DISTINCT Games) AS total_games_held
FROM olympic_history
),
games_participated AS (
SELECT DISTINCT
NOC,
COUNT(DISTINCT Games) AS total_games_participated
FROM olympic_history
GROUP BY NOC
)
SELECT n.region,
g.total_games_participated
FROM games_participated AS g
JOIN noc_regions AS n
ON g.NOC = n.NOC
JOIN olympic_games_held AS o
ON g.total_games_participated = o.total_games_held
-- 6. Identify the sport which was played in all summer olympics.
-- How many summer olympics were held:
SELECT COUNT(DISTINCT Games) AS total_summer_olympics
FROM olympic_history
WHERE Season = 'Summer'
-- Sports played at all summer olympics:
SELECT Sport,
COUNT(DISTINCT Games) AS total_olympics_played_at
FROM olympic_history
WHERE Season = 'Summer'
GROUP BY Sport
HAVING COUNT(DISTINCT Games) = 29
-- OR
WITH
summer_olympics AS (
SELECT COUNT(DISTINCT Games) AS total_summer_olympics
FROM olympic_history
WHERE Season = 'Summer'
),
sports_played AS (
SELECT Sport,
COUNT(DISTINCT Games) AS total_olympics_played_at
FROM olympic_history
GROUP BY Sport
)
SELECT p.Sport,
p.total_olympics_played_at
FROM sports_played AS p
JOIN summer_olympics AS s
ON total_summer_olympics = total_olympics_played_at
-- 7. Which Sports were just played once in the olympics?
SELECT Sport,
COUNT(DISTINCT Games) AS total_olympics_played_at
FROM olympic_history
GROUP BY Sport
HAVING COUNT(DISTINCT Games) = 1
-- 7.1 What olympics do they correspond to?
WITH
sports_played AS (
SELECT Sport,
COUNT(DISTINCT Games) AS total_olympics_played_at
FROM olympic_history
GROUP BY Sport
HAVING COUNT(DISTINCT Games) = 1
)
SELECT DISTINCT
s.Sport,
s. total_olympics_played_at,
o. Games,
o. City
FROM sports_played AS s
JOIN olympic_history AS o
ON s.Sport = o.Sport
-- 8. Fetch the total number of sports played in each olympic games.
SELECT Games,
COUNT(DISTINCT Sport) AS total_sports_played
FROM olympic_history
GROUP BY Games
ORDER BY total_sports_played DESC
-- 9. Fetch oldest athletes to win a gold medal.
WITH
gold_medals AS (
SELECT MAX(Age) AS Age
FROM olympic_history
WHERE Medal = 'Gold'
)
SELECT o.Name,
o.Sex,
o.Age,
o.Team,
o.Games,
o.City,
o.Sport,
o.Event,
o.Medal
FROM olympic_history AS o
JOIN gold_medals AS g
ON o.Age = g.Age
WHERE o.Medal = 'Gold'
-- OR
SELECT Name,
Sex,
Age,
Team,
Games,
City,
Sport,
Event,
Medal
FROM olympic_history
WHERE Medal = 'Gold'
AND Age = (
SELECT MAX(Age)
FROM olympic_history
WHERE Medal = 'Gold'
)
-- 10. Find the Ratio of female to male athletes that participated in all olympic games.
WITH
participants_by_sex AS (
SELECT CAST(SUM(CASE WHEN Sex = 'F' THEN 1 ELSE 0 END)AS FLOAT) AS female_participants,
CAST(SUM(CASE WHEN Sex = 'M' THEN 1 ELSE 0 END)AS FLOAT) AS male_participants
FROM olympic_history
)
SELECT CONCAT(female_participants/female_participants, ' : ', ROUND(male_participants/female_participants, 2)) AS ratio
FROM participants_by_sex
-- 11. Fetch the top 5 athletes who have won the most gold medals.
SELECT TOP 5
Name,
Team,
SUM(CASE WHEN Medal = 'Gold' THEN 1 ELSE 0 END) AS total_gold_medals
FROM olympic_history
GROUP BY Name, Team
ORDER BY total_gold_medals DESC
-- 12. Fetch the top 5 athletes who have won the most medals (gold/silver/bronze).
SELECT TOP 5
Name,
Team,
SUM(CASE WHEN Medal IN ('Gold', 'Silver', 'Bronze') THEN 1 ELSE 0 END) AS total_medals
FROM olympic_history
GROUP BY Name, Team
ORDER BY total_medals DESC
-- 13. Fetch the top 5 most successful countries in the olympics. Success is defined by no of medals won.
SELECT TOP 5
n.region,
SUM(CASE WHEN o.Medal IN ('Gold', 'Silver', 'Bronze') THEN 1 ELSE 0 END) AS total_medals
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY n.region
ORDER BY total_medals DESC
-- 14. List down total gold, silver and bronze medals won by each country.
SELECT n.region,
SUM(CASE WHEN o.Medal = 'Gold' THEN 1 ELSE 0 END) AS gold,
SUM(CASE WHEN o.Medal = 'Silver' THEN 1 ELSE 0 END) AS silver,
SUM(CASE WHEN o.Medal = 'Bronze' THEN 1 ELSE 0 END) AS bronze
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY n.region
ORDER BY gold DESC
-- 15. List down total gold, silver and bronze medals won by each country corresponding to each olympic games.
SELECT o.Games,
n.region,
SUM(CASE WHEN o.Medal = 'Gold' THEN 1 ELSE 0 END) AS gold,
SUM(CASE WHEN o.Medal = 'Silver' THEN 1 ELSE 0 END) AS silver,
SUM(CASE WHEN o.Medal = 'Bronze' THEN 1 ELSE 0 END) AS bronze
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY o.Games, n.region
ORDER BY o.Games
-- 16. Identify which country won the most gold, most silver and most bronze medals in each olympic games.
WITH
medals AS (
SELECT o.Games,
n.region,
SUM(CASE WHEN o.Medal = 'Gold' THEN 1 ELSE 0 END) AS gold,
SUM(CASE WHEN o.Medal = 'Silver' THEN 1 ELSE 0 END) AS silver,
SUM(CASE WHEN o.Medal = 'Bronze' THEN 1 ELSE 0 END) AS bronze
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY o.Games, n.region
)
SELECT DISTINCT
Games,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY gold DESC), ' - ' , first_value(gold) OVER(PARTITION BY games ORDER BY gold DESC)) AS max_gold,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY silver DESC), ' - ', first_value(silver) OVER(PARTITION BY games ORDER BY silver DESC)) AS max_silver,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY bronze DESC), ' - ', first_value(bronze) OVER(PARTITION BY games ORDER BY bronze DESC)) AS max_bronze
FROM medals
GROUP BY Games, region, gold, silver, bronze
-- 17. Identify which country won the most gold, most silver, most bronze medals as well as the most medals in each olympic games.
WITH
medals AS (
SELECT o.Games,
n.region,
SUM(CASE WHEN o.Medal = 'Gold' THEN 1 ELSE 0 END) AS gold,
SUM(CASE WHEN o.Medal = 'Silver' THEN 1 ELSE 0 END) AS silver,
SUM(CASE WHEN o.Medal = 'Bronze' THEN 1 ELSE 0 END) AS bronze,
SUM(CASE WHEN o.Medal IN ('Gold', 'Silver', 'Bronze') THEN 1 ELSE 0 END) AS total_medals
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY o.Games, n.region
)
SELECT DISTINCT
Games,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY gold DESC), ' - ' , first_value(gold) OVER(PARTITION BY games ORDER BY gold DESC)) AS max_gold,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY silver DESC), ' - ' , first_value(silver) OVER(PARTITION BY games ORDER BY silver DESC)) AS max_silver,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY bronze DESC), ' - ' , first_value(bronze) OVER(PARTITION BY games ORDER BY bronze DESC)) AS max_bronze,
CONCAT (first_value(region) OVER(PARTITION BY games ORDER BY total_medals DESC), ' - ' , first_value(total_medals) OVER(PARTITION BY games ORDER BY total_medals DESC)) AS max_medals
FROM medals
GROUP BY Games, region, gold, silver, bronze, total_medals
-- 18. Which countries have never a won gold medal but have won silver/bronze medals?
WITH
medals AS (
SELECT n.region,
SUM(CASE WHEN o.Medal = 'Gold' THEN 1 ELSE 0 END) AS gold,
SUM(CASE WHEN o.Medal = 'Silver' THEN 1 ELSE 0 END) AS silver,
SUM(CASE WHEN o.Medal = 'Bronze' THEN 1 ELSE 0 END) AS bronze
FROM olympic_history AS o
JOIN noc_regions AS n
ON o.NOC = n.NOC
GROUP BY n.region
)
SELECT DISTINCT
region,
gold,
silver,
bronze
FROM medals
WHERE gold = 0
AND (silver <> 0 OR bronze <> 0)
GROUP BY region, gold, silver, bronze
-- 19. In which Sport/event has India won the most medals?
SELECT TOP 1
Team AS Country,
Sport,
SUM(CASE WHEN Medal IN ('Gold', 'Silver', 'Bronze') THEN 1 ELSE 0 END) AS total_medals
FROM olympic_history
WHERE NOC = 'IND'
GROUP BY Team, Sport
ORDER BY total_medals DESC
-- 20. Break down all olympic games where India won a medal for Hockey, and show how many Hockey medals were won in each of those olympic games.
SELECT Games,
Team AS Country,
Sport,
SUM(CASE WHEN Medal IN ('Gold', 'Silver', 'Bronze') THEN 1 ELSE 0 END) AS total_medals
FROM olympic_history
WHERE NOC = 'IND'
AND Sport = 'Hockey'
AND Medal <> 'NA'
GROUP BY Games, Team, Sport
ORDER BY total_medals DESC