-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlsakila2.sql
352 lines (324 loc) · 10.6 KB
/
sqlsakila2.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
SET lc_time_names = 'fr_FR';
# Question 1
SELECT MONTHNAME(rental_date) as mois
FROM rental
WHERE year(rental_date) = 2006;
# Question 2a
# SELECT rental_duration
# FROM film;
# Question 2b
SELECT f.title, TIMEDIFF(return_date, rental_date) as rental_duration_hours
FROM rental
INNER JOIN inventory iv ON iv.inventory_id = rental.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id;
# Question 3
SELECT f.title, f.description, DATE_FORMAT(rental.rental_date, "%W %D %M %Y à %H heures %i minutes et %s secondes") as dateformat
FROM rental
INNER JOIN inventory inv ON inv.inventory_id = rental.inventory_id
INNER JOIN film f ON f.film_id = inv.film_id
WHERE HOUR(rental_date) < 1 and YEAR(rental_date) = 2005;
# Question 4
SELECT f.title, f.description, rental_date
FROM rental
INNER JOIN inventory inv ON inv.inventory_id = rental.inventory_id
INNER JOIN film f ON f.film_id = inv.film_id
WHERE MONTH(rental_date) BETWEEN 4 AND 5;
# Question 5a
SELECT title
FROM film
WHERE LOWER(title) REGEXP "^(?!le).+$";
# Question 5b (avec "le ")
SELECT title
FROM film
WHERE LOWER(title) REGEXP "^(?!le ).+$";
# Question 6
SELECT title, IF(rating = "NC-17", "oui", "non") as is_nc_17
FROM film
WHERE rating IN ("PG-13", "NC-17");
# Question 7
SELECT *
FROM category
WHERE LOWER(name) REGEXP "^[a|c]";
# Question 8
SELECT substr(name, 1, 3) as short
FROM category;
# Question 9
SELECT REPLACE(UPPER(first_name), "A", "E"), last_name
FROM actor
ORDER BY last_name ASC;
# Question 10
SELECT title, lang.name
FROM film
INNER JOIN language lang ON lang.language_id = film.language_id
LIMIT 10;
# Question 11
SELECT f.title, a.first_name, a.last_name, f.release_year
FROM actor a
INNER JOIN film_actor fa ON fa.actor_id = a.actor_id
INNER JOIN film f ON f.film_id = fa.film_id
WHERE UPPER(a.first_name) = "JENNIFER" AND UPPER(a.last_name) = "DAVIS" AND f.release_year = "2006";
# Question 12
SELECT c.first_name, c.last_name
FROM film f
INNER JOIN inventory iv ON iv.film_id = f.film_id
INNER JOIN rental rt ON rt.inventory_id = iv.inventory_id
INNER JOIN customer c ON c.customer_id = rt.customer_id
WHERE f.title = "ALABAMA DEVIL";
# Question 13
SELECT DISTINCT(f.title)
FROM city ct
INNER JOIN address addr ON addr.city_id = ct.city_id
INNER JOIN customer c ON c.address_id = addr.address_id
INNER JOIN rental rt ON rt.customer_id = c.customer_id
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
WHERE ct.city = "Woodridge";
# Question 14
SELECT DISTINCT(f.title), rt.rental_date, rt.return_date, TIMEDIFF(rt.return_date, rt.rental_date) as duration
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
WHERE rt.return_date IS NOT NULL
ORDER BY duration ASC, f.title ASC
LIMIT 10;
# Question 15
SELECT f.title, c.name
FROM category c
INNER JOIN film_category fc ON fc.category_id = c.category_id
INNER JOIN film f ON f.film_id = fc.film_id
WHERE c.name = "Action"
ORDER BY f.title ASC;
# Question 16
SELECT DISTINCT(f.title), rt.rental_date, rt.return_date, (timestampdiff(SECOND, rt.rental_date, rt.return_date) / 3600) as duration
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
WHERE rt.return_date IS NOT NULL AND timestampdiff(HOUR, rt.rental_date, rt.return_date) < 48
ORDER BY duration ASC, f.title ASC;
# Pour aller plus loin
# 1
SELECT c.first_name, c.last_name, f.title, st.store_id, addr.address, TIMEDIFF(rt.return_date, rt.rental_date) as duration
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
INNER JOIN customer c ON c.customer_id = rt.customer_id
INNER JOIN store st ON st.store_id = iv.store_id
INNER JOIN address addr ON addr.address_id = st.address_id
ORDER BY duration DESC, c.last_name ASC
LIMIT 10;
# 2
SELECT c.first_name, c.last_name, sum(p.amount) as total_amount
FROM payment p
INNER JOIN customer c ON c.customer_id = p.customer_id
GROUP BY p.customer_id
ORDER BY c.last_name;
# 3
SELECT f.title, (AVG(timestampdiff(SECOND, rt.rental_date, rt.return_date)) / (3600 * 24)) as moyenne_duree
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
WHERE rt.return_date IS NOT NULL
GROUP BY f.film_id
ORDER BY moyenne_duree DESC;
SELECT f.title, AVG(TIMEDIFF(rt.return_date, rt.rental_date)) as moyenne_duree
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
WHERE rt.return_date IS NOT NULL
GROUP BY f.film_id
ORDER BY moyenne_duree DESC;
# 4a
SELECT *
FROM film
WHERE film_id NOT IN (
SELECT iv.film_id
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
)
ORDER BY title;
#4b
SELECT f.title, COUNT(rt.rental_id) as nb_rent
FROM inventory iv
RIGHT JOIN film f USING(film_id)
LEFT JOIN rental rt USING(inventory_id)
GROUP BY f.film_id
HAVING nb_rent = 0;
# 5
SELECT s.store_id, addr.address, COUNT(s.staff_id) as nb_employee
FROM staff s
INNER JOIN store st ON st.store_id = s.store_id
INNER JOIN address addr ON addr.address_id = st.address_id
GROUP BY s.store_id;
# 6
SELECT ct.city_id, ct.city, COUNT(addr.address) as nb_store
FROM store st
INNER JOIN address addr ON addr.address_id = st.address_id
INNER JOIN city ct ON ct.city_id = addr.city_id
GROUP BY ct.city_id
ORDER BY nb_store DESC
LIMIT 10;
# 7
SELECT f.title, f.length
FROM actor a
INNER JOIN film_actor fa ON fa.actor_id = a.actor_id
INNER JOIN film f ON f.film_id = fa.film_id
WHERE LOWER(a.first_name) = "johnny" AND LOWER(a.last_name) = "lollobrigida"
ORDER BY f.length DESC, f.title ASC
LIMIT 1;
# 8
SELECT f.film_id, f.title, (AVG(timestampdiff(SECOND, rt.rental_date, rt.return_date)) / (3600 * 24)) as moyenne_location
FROM film f
INNER JOIN inventory iv ON iv.film_id = f.film_id
INNER JOIN rental rt ON rt.inventory_id = iv.inventory_id
WHERE LOWER(f.title) = "academy dinosaur" AND rt.return_date IS NOT NULL
GROUP BY f.film_id, f.title;
# 9a
SELECT *
FROM (
SELECT iv.film_id, iv.store_id, f.title, COUNT(*) as nb_exemplaire
FROM inventory iv
INNER JOIN film f ON f.film_id = iv.film_id
GROUP BY iv.store_id, iv.film_id
) s2
WHERE s2.nb_exemplaire > 2;
# 9b
SELECT iv.film_id, iv.store_id, f.title, COUNT(*) as nb_exemplaire
FROM inventory iv
INNER JOIN film f ON f.film_id = iv.film_id
GROUP BY iv.store_id, iv.film_id
HAVING nb_exemplaire > 2;
# 10
SELECT *
FROM film
WHERE LOWER(title) REGEXP "din"
ORDER BY title ASC;
# 11
SELECT f.film_id, f.title, COUNT(rt.rental_id) as nb_rentals
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
GROUP BY f.film_id
ORDER BY nb_rentals DESC
LIMIT 5;
# 12
SELECT *
FROM film
WHERE release_year IN (2003, 2005, 2006)
ORDER BY release_year ASC, title ASC;
# 13
SELECT rt.rental_id, rt.rental_date, f.title, c.first_name, c.last_name
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
INNER JOIN customer c ON c.customer_id = rt.customer_id
WHERE rt.return_date IS NULL
ORDER BY rt.rental_date ASC, f.title ASC
LIMIT 10;
# 14
SELECT *
FROM category c
INNER JOIN film_category fc ON fc.category_id = c.category_id
INNER JOIN film f ON f.film_id = fc.film_id
WHERE LOWER(c.name) = "action"
ORDER BY f.title ASC;
# 15
SELECT DISTINCT(CONCAT(c.first_name, " ", c.last_name)) as customer_name
FROM film f
INNER JOIN inventory iv ON iv.film_id = f.film_id
INNER JOIN rental rt ON rt.inventory_id = iv.inventory_id
INNER JOIN customer c ON c.customer_id = rt.customer_id
WHERE f.rating = "NC-17"
ORDER BY customer_name;
# 16a (langues originelles)
SELECT *
FROM category c
INNER JOIN film_category fc ON fc.category_id = c.category_id
INNER JOIN film f ON f.film_id = fc.film_id
INNER JOIN language lg ON lg.language_id = f.original_language_id
WHERE LOWER(c.name) = "animation" AND LOWER(lg.name) = "english";
# 16b (pour les langues pas originelles)
SELECT *
FROM category c
INNER JOIN film_category fc ON fc.category_id = c.category_id
INNER JOIN film f ON f.film_id = fc.film_id
INNER JOIN language lg ON lg.language_id = f.language_id
WHERE LOWER(c.name) = "animation" AND LOWER(lg.name) = "english";
# 17a
SELECT f.title
FROM actor a
INNER JOIN film_actor fa USING(actor_id)
INNER JOIN film f USING(film_id)
WHERE LOWER(a.first_name) = "jennifer"
ORDER BY f.title ASC;
# 17b1
SELECT *
FROM actor a
INNER JOIN film_actor fa USING(actor_id)
INNER JOIN film f USING(film_id)
WHERE LOWER(a.first_name) = "johnny" AND f.film_id IN
(
SELECT f.film_id
FROM actor a
INNER JOIN film_actor fa USING(actor_id)
INNER JOIN film f USING(film_id)
WHERE LOWER(a.first_name) = "jennifer"
);
# 17b2
SELECT f.film_id, f.title, IF(COUNT(*) = 2, "oui", "non") as is_duo
FROM actor a
INNER JOIN film_actor fa USING(actor_id)
INNER JOIN film f USING(film_id)
WHERE LOWER(a.first_name) IN ("jennifer", "johnny")
GROUP BY f.film_id
HAVING is_duo = "oui"
ORDER BY f.film_id;
# 18
SELECT c.name, COUNT(*) as nb_rental_by_cat
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN film f ON f.film_id = iv.film_id
INNER JOIN film_category fc ON fc.film_id = f.film_id
INNER JOIN category c ON c.category_id = fc.category_id
GROUP BY c.category_id
ORDER BY nb_rental_by_cat DESC
LIMIT 3;
# 19
SELECT ct.city, COUNT(*) as nb_rental_by_cit
FROM rental rt
INNER JOIN inventory iv ON iv.inventory_id = rt.inventory_id
INNER JOIN store st ON st.store_id = iv.store_id
INNER JOIN address addr ON addr.address_id = st.address_id
INNER JOIN city ct ON ct.city_id = addr.city_id
GROUP BY ct.city_id
ORDER BY nb_rental_by_cit DESC
LIMIT 10;
# 19b: version avec la ville des clients
SELECT ct.city, COUNT(*) as nb_rental_by_cit
FROM rental rt
INNER JOIN customer c ON c.customer_id = rt.customer_id
INNER JOIN address addr ON addr.address_id = c.address_id
INNER JOIN city ct ON ct.city_id = addr.city_id
GROUP BY ct.city_id
ORDER BY nb_rental_by_cit DESC
LIMIT 10;
# 20
SELECT a.actor_id, a.first_name, a.last_name, COUNT(*) as nb_movie_acted
FROM film_actor fa
INNER JOIN actor a ON a.actor_id = fa.actor_id
GROUP BY a.actor_id
# HAVING nb_movie_acted >= 1 not necessary since film_actor only list association
ORDER BY nb_movie_acted DESC, a.last_name ASC;
# 21 : Afficher les acteurs qui n'ont joué dans aucun film
SELECT *
FROM actor
WHERE actor_id NOT IN (
SELECT DISTINCT(fa.actor_id)
FROM film_actor fa
)
ORDER BY last_name ASC;
# 21 : Afficher les acteurs qui n'ont joué dans aucun film
SELECT *
FROM actor a
LEFT JOIN film_actor fa USING(actor_id)
WHERE fa.actor_id IS NULL
ORDER BY a.last_name ASC;