-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises12.sql
67 lines (61 loc) · 1.16 KB
/
exercises12.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
-- Q1
SELECT COUNT(*) FROM film
WHERE length >
(
SELECT AVG(length) FROM film
);
-- Q2
SELECT COUNT(*) FROM film
WHERE rental_rate =
(
SELECT MAX(rental_rate) FROM film
);
-- Q3
SELECT * FROM film
WHERE rental_rate =
(
SELECT MIN(rental_rate) FROM film
)
AND
replacement_cost =
(
SELECT MIN(replacement_cost) FROM film
);
-- Q3 (alternative to use ALL/ANY)
SELECT * FROM film
WHERE film_id = ANY
(
SELECT film_id FROM film
WHERE rental_rate =
(
SELECT MIN(rental_rate) FROM film
)
AND
replacement_cost =
(
SELECT MIN(replacement_cost) FROM film
)
);
-- Q4
SELECT * FROM customer
WHERE customer_id = ANY
(
SELECT customer_id FROM payment
GROUP BY customer_id
HAVING COUNT(*) =
(
SELECT MAX(count_nums) FROM
(
SELECT customer_id, COUNT(*) AS count_nums FROM payment
GROUP BY customer_id
) AS counts
)
);
-- Q4 (alternative but this one fails when there are multiple customers with same count of payments)
SELECT customer.customer_id, customer.first_name, customer.last_name
FROM payment
JOIN customer
ON payment.customer_id = customer.customer_id
GROUP BY customer.customer_id, customer.first_name, customer.last_name
ORDER BY COUNT(*) DESC
LIMIT 1;