-
Notifications
You must be signed in to change notification settings - Fork 0
/
requetes.sql
289 lines (266 loc) · 5.51 KB
/
requetes.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
-- Exercice 3
--
-- Q1
-- Output: 15
SELECT count(*)
FROM information_schema.columns
WHERE
table_schema = USER
AND table_name = 'import_athletes';
-- Q2
-- Output: 255080
SELECT count(*)
FROM import_athletes;
-- Q3
-- Output: 231 (avec l'ajout de Singapour)
SELECT count(noc)
FROM import_noc;
-- Q4
-- Output: 127575
SELECT count(DISTINCT id)
FROM import_athletes;
-- Q5
-- Output: 12116
SELECT count(medal)
FROM import_athletes
WHERE medal = 'Gold';
-- Q6
-- Output: 2
SELECT count(*)
FROM import_athletes
WHERE name LIKE 'Carl Lewis%';
-- Exercice 5
--
-- Q1
SELECT
r.nom_pays,
count(*) AS nb_participations
FROM
participe AS p,
region AS r
WHERE
r.noc = p.noc
AND p.nom_equipe = r.nom_equipe
GROUP BY r.nom_pays
ORDER BY count(*) DESC;
-- Q2
SELECT
r.nom_pays,
count(p.medaille) AS nb_medailles_Or
FROM
participe AS p,
region AS r
WHERE
p.medaille = 'Gold'
AND p.noc = r.noc
AND p.nom_equipe = r.nom_equipe
GROUP BY r.nom_pays
ORDER BY count(p.medaille) DESC;
-- Q3
SELECT
r.nom_pays,
count(p.medaille) AS nb_medailles_pays
FROM
participe AS p,
region AS r
WHERE
p.medaille IS NOT NULL
AND p.noc = r.noc
AND p.nom_equipe = r.nom_equipe
GROUP BY r.nom_pays
ORDER BY count(p.medaille) DESC;
-- Q4
SELECT
a.ano,
a.nom,
count(p.medaille) AS nb_medailles_Or
FROM
athlete AS a
JOIN participe AS p ON a.ano = p.ano
WHERE p.medaille = 'Gold'
GROUP BY
a.ano,
a.nom
ORDER BY
count(p.medaille) DESC;
-- Q5
SELECT
r.nom_pays,
count(p.medaille) AS nb_medailles_Albertville
FROM
participe AS p,
edition AS e,
region AS r
WHERE
e.ville = 'Albertville'
AND p.medaille IS NOT NULL
AND e.annee = p.annee
AND e.saison = p.saison
AND p.noc = r.noc
AND p.nom_equipe = r.nom_equipe
GROUP BY r.nom_pays
ORDER BY count(p.medaille) DESC;
-- Q6
SELECT DISTINCT p.ano
FROM
participe AS p,
region AS r,
region AS r2
WHERE
p.noc = r.noc
AND p.nom_equipe = r.nom_equipe
AND p.noc = r2.noc
AND p.nom_equipe = r2.nom_equipe
AND r.nom_pays <> r2.nom_pays
AND r.nom_pays = 'France';
-- Q7
-- Même requête que la Q6
-- Q8
SELECT
a.age,
count(p.medaille) nb_medailles_Or
FROM
athlete AS a
JOIN participe AS p ON a.ano = p.ano
WHERE
p.medaille = 'Gold'
AND a.age IS NOT NULL
GROUP BY a.age
ORDER BY count(p.medaille) DESC;
-- Q9
SELECT
e.nom_sport,
count(p.medaille) AS nb_médailles_plus_de_50ans
FROM
epreuve AS e,
athlete AS a,
participe AS p
WHERE
a.age >= 50
AND e.evenement = p.evenement
AND e.nom_sport = p.nom_sport
AND e.genre = p.genre
AND a.ano = p.ano
GROUP BY e.nom_sport
ORDER BY count(p.medaille) DESC;
-- Q10
SELECT
ed.annee,
ed.saison,
count(ep.evenement) nb_epreuves
FROM
edition AS ed,
epreuve AS ep,
participe AS p
WHERE
ep.evenement = p.evenement
AND ep.nom_sport = p.nom_sport
AND ep.genre = p.genre
AND ed.annee = p.annee
AND ed.saison = p.saison
GROUP BY
ed.annee,
ed.saison
ORDER BY ed.annee ASC;
-- Q11
SELECT
ed.annee,
count(p.medaille) AS nb_médailles_femme_en_été
FROM
edition AS ed,
participe AS p,
athlete AS a
WHERE
a.sexe = 'F'
AND ed.saison = 'Summer'
AND ed.annee = p.annee
AND ed.saison = p.saison
AND a.ano = p.ano
GROUP BY ed.annee
ORDER BY ed.annee ASC;
-- Exercice 6
--
-- Sport : Basketball
-- Pays : USA
--
-- Requête n°1 : Moyenne d'âge des athletes masculins
SELECT avg(a.age) AS Moyenne_age
FROM
athlete AS a,
epreuve AS e,
region AS r,
participe AS p
WHERE
r.nom_pays = 'USA'
AND e.nom_sport = 'Basketball'
AND a.sexe = 'M'
AND a.ano = p.ano
AND e.evenement = p.evenement
AND e.nom_sport = p.nom_sport
AND e.genre = p.genre
AND r.noc = p.noc
AND r.nom_equipe = p.nom_equipe;
-- Requête n°2 : Athlètes ayant participé au moins 3 fois à une édition
SELECT a.ano, a.nom, count(*)
FROM
athlete AS a,
epreuve AS e,
region AS r,
participe AS p,
edition as ed
WHERE
r.nom_pays = 'USA'
AND e.nom_sport = 'Basketball'
AND a.ano = p.ano
AND e.evenement = p.evenement
AND e.nom_sport = p.nom_sport
AND e.genre = p.genre
AND r.noc = p.noc
AND r.nom_equipe = p.nom_equipe
AND ed.annee = p.annee
AND ed.saison = p.saison
GROUP BY a.ano, a.nom
HAVING count(*) >= 3;
-- Requête n°3 : Athlètes qui ont participé à 3 éditions d'affilé
SELECT a.nom, ed.annee, ed2.annee, ed3.annee
FROM
athlete AS a,
epreuve AS e,
region AS r,
participe AS p,
edition AS ed,
edition AS ed2,
edition AS ed3
WHERE
r.nom_pays = 'USA'
AND e.nom_sport = 'Basketball'
AND a.ano = p.ano
AND e.evenement = p.evenement
AND e.nom_sport = p.nom_sport
AND e.genre = p.genre
AND r.noc = p.noc
AND r.nom_equipe = p.nom_equipe
AND ed.annee = p.annee
AND ed.saison = p.saison
AND ed2.annee = ed.annee+4
AND ed2.saison = p.saison
AND ed3.annee = ed2.annee+4
AND ed3.saison = p.saison;
-- Requête n°4 : Athlètes classés par taille du plus petit au plus grand
SELECT
a.nom,
a.taille
FROM
athlete AS a
JOIN participe AS p
ON a.ano = p.ano
JOIN region AS r
ON r.noc = p.noc AND r.nom_equipe = p.nom_equipe
JOIN epreuve AS e
ON e.evenement = p.evenement
AND e.nom_sport = p.nom_sport
AND e.genre = p.genre
WHERE r.nom_pays = 'USA'
AND e.nom_sport = 'Basketball'
AND a.taille IS NOT NULL
ORDER BY a.taille ASC;