-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queries.txt
111 lines (83 loc) · 4.85 KB
/
Queries.txt
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
/* Query1 For Question 1 set 1*/
/* We want to understand more about the movies that families are watching. The following categories are considered family movies: Animation, Children, Classics, Comedy, Family and Music.
Create a query that lists each movie, the film category it is classified in, and the number of times it has been rented out.
Check Your Solution
For this query, you will need 5 tables: Category, Film_Category, Inventory, Rental and Film. Your solution should have three columns: Film title, Category name and Count of Rentals.
The following table header provides a preview of what the resulting table should look like if you order by category name followed by the film title.
HINT: One way to solve this is to create a count of movies using aggregations, subqueries and Window functions.*/
SELECT f.title Film_title, c.name Category_name, COUNT(r.rental_id) AS Rental_Count
FROM film f
JOIN film_category fc
ON f.film_id = fc.film_id
JOIN Category c
ON c.category_id = fc.category_id
JOIN Inventory i
ON f.film_id = i.film_id
JOIN rental r
ON r.inventory_id = i.inventory_id
WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')
GROUP BY f.title, c.name
ORDER BY c.name, f.title;
/* Query2 For Question 2 set 1*/
/* Question 2
Now we need to know how the length of rental duration of these family-friendly movies compares to the duration that all movies are rented for. Can you provide a table with the movie titles and divide them into 4 levels (first_quarter, second_quarter, third_quarter, and final_quarter) based on the quartiles (25%, 50%, 75%) of the rental duration for movies across all categories? Make sure to also indicate the category that these family-friendly movies fall into.
Check Your Solution
The data are not very spread out to create a very fun looking solution, but you should see something like the following if you correctly split your data. You should only need the category, film_category, and film tables to answer this and the next questions.
HINT: One way to solve it requires the use of percentiles, Window functions, subqueries or temporary tables.
SELECT f.title Film_title, c.name Category_name, f.rental_duration Rental_Duration, COUNT(4) OVER (ORDER BY f.rental_duration) AS standard_quartile
FROM film f
JOIN film_category fc
ON f.film_id = fc.film_id
JOIN category c
ON c.category_id = fc.category_id
WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')
ORDER BY f.rental_duration ASC
/* Query3 For Question 3 set 1 */
/* Question 3
Finally, provide a table with the family-friendly film category, each of the quartiles, and the corresponding count of movies within each combination of film category for each corresponding rental duration category. The resulting table should have three columns:
Category
Rental length category
Count
Check Your Solution
The following table header provides a preview of what your table should look like. The Count column should be sorted first by Category and then by Rental Duration category.
HINT: One way to solve this question requires the use of Percentiles, Window functions and Case statements.
*/
SELECT n1.name, n1.standard_quartile, COUNT(n1.standard_quartile)
FROM
(SELECT f.title, c.name , f.rental_duration, NTILE(4) OVER (ORDER BY f.rental_duration) AS standard_quartile
FROM film f
JOIN film_category fc
ON f.film_id = fc.film_id
JOIN category c
ON c.category_id = fc.category_id
WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')) n1
GROUP BY 1, 2
ORDER BY 1, 2;
*/
/* Query4 For Question 2 set 2 */
/*
Question 2
We would like to know who were our top 10 paying customers, how many payments they made on a monthly basis during 2007, and what was the amount of the monthly payments. Can you write a query to capture the customer name, month and year of payment, and total payment amount for each month by these top 10 paying customers?
Check your Solution:
The following table header provides a preview of what your table should look like. The results are sorted first by customer name and then for each month. As you can see, total amounts per month are listed for each customer.
HINT: One way to solve is to use a subquery, limit within the subquery, and use concatenation to generate the customer name.
*/
SELECT DATE_TRUNC('month', p.payment_date) pay_month, c.first_name || ' ' || c.last_name AS full_name, COUNT(p.amount) AS pay_countpermon, SUM(p.amount) AS pay_amount
FROM payment p
JOIN customer c
ON c.customer_id = p.customer_id
WHERE c.first_name || ' ' || c.last_name IN
(SELECT n1.full_name
FROM
(SELECT c.first_name || ' ' || c.last_name AS full_name, SUM(p.amount) as amount_total
FROM payment p
JOIN customer c
ON c.customer_id = p.customer_id
GROUP BY 1
ORDER BY 2 DESC
LIMIT 10) n1) AND (p.payment_date BETWEEN '2007-01-01' AND '2008-01-01')
GROUP BY 2,
1
ORDER BY 2,
1,
3