-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUdemy 1 - SQL PosteGre SQL
477 lines (315 loc) · 9.68 KB
/
Udemy 1 - SQL PosteGre 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
Conseil :
creer un doc ou apparait toutes les tables avec les clés et les variables
Cette plateforme est super pour cela :
https://www.xmind.net/fr/
Diverses requetes SQL pour apprendre
--Select
SELECT * FROM actor;
SELECT title FROM film;
SELECT first_name, last_name FROM actor;
SELECT customer_id,last_name,first_name, email FROM customer;
--cdt avec where
SELECT * FROM actor
WHERE first_name = 'Nick' AND last_name='Stallone';
SELECT amount FROM payment
WHERE amount > 3;
SELECT email, first_name,last_name
FROM customer
WHERE store_id = 2;
SELECT email
FROM customer
WHERE first_name = 'Maria' ;
--trie avec order by
SELECT first_name, last_name
FROM customer
ORDER BY first_name ASC, last_name DESC;
--limit le nbr de resultat
SELECT first_name,last_name
FROM customer
LIMIT 3;
--valeur unique (no doublon) distinct
SELECT DISTINCT rental_rate
FROM film;
SELECT DISTINCT rating
FROM film;
--COUNT
SELECT COUNT (DISTINCT rating)
FROM film;
SELECT COUNT (rental_rate)
FROM film
WHERE rental_rate =4.99;
SELECT COUNT (rental_rate)
FROM film
WHERE rental_rate > 1;
SELECT rental_id, customer_id,return_date
FROM rental
WHERE customer_id IN (3,6 , 1,2)
ORDER BY return_date;
SELECT title , rental_rate
FROM film
WHERE rental_rate IN (0.99,4.99);
-- Retourne tout les cellules qui commmence par li
SELECT first_name
FROM customer
WHERE first_name LIKE 'Li%';
-- retourne les cellules qui commence par li et qui a 2 lettres apres
SELECT first_name
FROM customer
WHERE first_name LIKE 'li__';
SELECT COUNT (first_name)
FROM actor
WHERE first_name LIKE 'P%';
--like retourne les cellules qui comprend truman dans la chaine de car
SELECT COUNT (title )
FROM film
WHERE title LIKE '%Truman%';
--between retourne les results qui sont compris dans les bornes inclusees
SELECT customer_id, amount
FROM payment
WHERE amount NOT BETWEEN 7 AND 8;
--retourne les resultats dont les datessont comprises dans les bornes
SELECT customer_id,amount,payment_date
FROM payment
WHERE payment_date BETWEEN '2007-02-15' AND '2017-02-20';
-- retourne le min
SELECT MIN ( amount )
FROM payment;
-- retourne le max
SELECT MAX ( amount )
FROM payment;
--retourne la moyenne
SELECT ROUND ( AVG ( amount ), 1) AS AVG_amount
FROM payment;
SELECT COUNT ( amount )
FROM payment
WHERE amount !=0.0 ;
--retourne la somme
SELECT SUM (amount)
FROM payment ;
-- order by retourne une table groupée avec les fct d aggregations
SELECT customer_id , COUNT (*) as loc
FROM rental
GROUP BY customer_id
ORDER BY loc DESC;
SELECT customer_id , SUM (amount) as amount
FROM payment
GROUP BY customer_id
ORDER BY amount DESC
LIMIT 10;
SELECT rating , ROUND (AVG (rental_rate), 2) AS note
FROM film
GROUP BY rating
ORDER BY note DESC;
SELECT staff_id, COUNT(amount) AS nbre, SUM (amount) AS somme
FROM payment
GROUP BY staff_id
ORDER BY somme;
SELECT customer_id , SUM (amount) AS somme , COUNT (amount) AS nbre
FROM payment
GROUP BY customer_id
ORDER BY somme DESC
LIMIT 3 ;
-- having : cdt sur les lignes groupés
SELECT customer_id , SUM (amount) AS somme , COUNT (amount) AS nbre
FROM payment
GROUP BY customer_id
HAVING SUM (amount) > 160 ;
SELECT rating, ROUND (AVG (rental_rate), 2) AS note
FROM film
WHERE rating IN ('R', 'G','PG' )
GROUP BY rating
HAVING ROUND (AVG (rental_rate), 2) > 2.9 ;
-- inner join retourne une table a partir de plusieurs tables
SELECT SUM(amount) , ROUND (AVG (payment.customer_id),0) as id_, first_name, last_name
FROM payment
INNER JOIN customer
ON payment.customer_id = customer.customer_id
GROUP BY first_name , last_name
ORDER BY last_name , first_name ;
SELECT amount , p.customer_id , first_name, last_name
FROM payment AS p
INNER JOIN customer AS c
ON p.customer_id = c.customer_id
WHERE p.customer_id = 7 ;
SELECT title, store_id, COUNT (title)
FROM inventory AS i
INNER JOIN film AS f
ON i.film_id = f.film_id
GROUP BY store_id, title;
SELECT r.customer_id, title, store_id , return_date
FROM inventory AS i
INNER JOIN film AS f
ON i.film_id = f.film_id
INNER JOIN rental AS r
ON i.inventory_id = r.inventory_id ;
SELECT f.film_id, title, first_name, last_name
FROM film AS f
INNER JOIN film_actor AS fa
ON f.film_id = fa.film_id
INNER JOIN actor AS a
ON a.actor_id = fa.actor_id
WHERE f.title = 'Academy Dinosaur';
SELECT f.film_id, title,inventory_id,store_id
FROM film AS f
LEFT OUTER JOIN inventory AS i
ON i.film_id = f.film_id
WHERE title = 'Academy Dinosaur';
SELECT title , c.name AS categorie , l.name as Langue
FROM film AS f
INNER JOIN film_category AS fc
ON f.film_id = fc.film_id
INNER JOIN category AS c
ON c.category_id = fc.category_id
INNER JOIN language AS l
ON f.language_id = l.language_id
;
SELECT ROUND (AVG (fa.actor_id)) AS id,first_name,last_name, COUNT (fa.actor_id) AS nbr
FROM film AS f
INNER JOIN film_actor AS fa
ON fa.film_id = f.film_id
INNER JOIN actor AS a
ON a.actor_id = fa.actor_id
GROUP BY first_name,last_name
ORDER BY nBr DESC;
SELECT first_name,last_name, COUNT (title) as nbr
FROM film_actor AS fa
INNER JOIN actor AS a
ON fa.actor_id = a.actor_id
INNER JOIN film AS f
ON f.film_id = fa.film_id
GROUP BY first_name,last_name
ORDER BY nbr DESC ;
SELECT first_name,last_name, COUNT (title) AS nb
FROM (SELECT title, i.film_id
FROM inventory AS i
INNER JOIN film AS f
ON i.film_id = f.film_id
WHERE store_id = 1) AS ta
INNER JOIN (SELECT first_name,last_name, film_id
FROM film_actor AS fa
INNER JOIN actor AS a
ON fa.actor_id = a.actor_id ) AS tb
ON ta.film_id = tb.film_id
GROUP BY first_name,last_name
ORDER BY nb desc
;
SELECT title, COUNT (i.film_id) AS nb
FROM inventory AS i
INNER JOIN film AS f
ON i.film_id = f.film_id
WHERE store_id = 1
GROUP BY title
HAVING COUNT (i.film_id) > 3
ORDER BY nb DESC
;
SELECT country , city_id
FROM city AS ci
INNER JOIN country AS cou
ON cou.country_id = ci.country_id ;
SELECT customer_id , city_id
FROM customer AS c
INNER JOIN address AS a
ON c.address_id = a.address_id ;
-- retourne le nombre de client / pays
SELECT country , COUNT ( customer_id) as nb
FROM ( SELECT country , city_id
FROM city AS ci
INNER JOIN country AS cou
ON cou.country_id = ci.country_id ) AS ta
INNER JOIN ( SELECT customer_id , city_id
FROM customer AS c
INNER JOIN address AS a
ON c.address_id = a.address_id) AS tb
ON ta.city_id = tb.city_id
GROUP BY country
ORDER BY nb DESC
;
SELECT country, COUNT (customer_id) AS nb
FROM customer AS c
INNER JOIN address AS a
ON c.address_id = a.address_id
INNER JOIN city AS ci
ON ci.city_id = a.city_id
INNER JOIN country AS cou
ON cou.country_id = ci.country_id
GROUP BY country
ORDER BY nb DESC
LIMIT 3;
SELECT first_name,last_name,email, COUNT ( rental_id) AS nb
FROM customer AS c
INNER JOIN rental AS r
ON c.customer_id = r.customer_id
GROUP BY c.customer_id
HAVING COUNT ( r.rental_id) > 37
;
SELECT store_id , COUNT (rental_id)
FROM staff AS s
INNER JOIN rental AS r
ON s.staff_id = r.staff_id
GROUP BY store_id;
SELECT store_id , SUM (p.amount)
FROM staff AS s
INNER JOIN rental AS r
ON s.staff_id = r.staff_id
INNER JOIN payment AS p
ON p.customer_id = r.customer_id
GROUP BY store_id
ORDER BY SUM (p.amount)DESC ;
SELECT i.store_id, SUM (p.amount)
FROM inventory AS i
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
INNER JOIN payment AS p ON p.customer_id = r.customer_id
GROUP BY i.store_id;
SELECT store_id, SUM (p.amount) AS revenue
FROM inventory AS i
INNER JOIN rental AS r ON r.inventory_id = i.inventory_id
INNER JOIN payment AS p ON p.rental_id = r.rental_id
GROUP BY i.store_id;
--CBm de loc /cat de film (exclure comedy et animation)
-- quelle est la meilleure categ
-- afficher le total de loc avec la categorie de film
SELECT cat.name, COUNT (r.rental_id) AS nb
FROM film_category AS fc
INNER JOIN category AS cat ON fc.category_id = cat.category_id
INNER JOIN film AS f ON fc.film_id = f.film_id
INNER JOIN inventory AS i ON f.film_id = i.film_id
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
WHERE cat.name NOT IN ('Animation', 'Comedy')
GROUP BY cat.name
ORDER BY COUNT (r.rental_id) DESC;
-- category la + rentable
-- afficher category, montant
SELECT cat.name, SUM (p.amount)
FROM film_category AS fc
INNER JOIN category AS cat ON fc.category_id = cat.category_id
INNER JOIN film AS f ON fc.film_id = f.film_id
INNER JOIN inventory AS i ON f.film_id = i.film_id
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
INNER JOIN payment AS p ON r.rental_id = p.rental_id
GROUP BY cat.name
ORDER BY SUM (p.amount) DESC;
-- afficher film en langue anglaise qui ont + de 30 location
SELECT f.title , COUNT (r.rental_id) AS nb
FROM film AS f
INNER JOIN language AS l ON f.language_id = l.language_id
INNER JOIN inventory AS i ON f.film_id = i.film_id
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
WHERE l.name IN ('English')
GROUP BY f.title
HAVING COUNT (r.rental_id) > 30
ORDER BY COUNT (r.rental_id) DESC ;
-- Trouver les film qui rapportent le +
-- show all film , nbr loc, revenu degages /film
SELECT f.title , SUM ( amount )
FROM film AS f
INNER JOIN inventory AS i ON f.film_id = i.film_id
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
INNER JOIN payment AS p ON p.rental_id = r.rental_id
GROUP BY f.title
ORDER BY SUM ( amount ) DESC;
SELECT f.title, COUNT (r.rental_id) , f.rental_rate * COUNT (r.rental_id) AS ca
FROM film AS f
INNER JOIN inventory AS i ON f.film_id = i.film_id
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
GROUP BY f.title , rental_rate
ORDER BY rental_rate * COUNT (r.rental_id)DESC;