/
MavenMovies_Final_Course_Project.sql
234 lines (156 loc) · 5.36 KB
/
MavenMovies_Final_Course_Project.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
/*
1. My partner and I want to come by each of the stores in person and meet the managers.
Please send over the managers’ names at each store, with the full address
of each property (street address, district, city, and country please).
*/
SELECT
staff.first_name,
staff.last_name,
address.address,
address.district,
city.city,
country.country
FROM
staff
INNER JOIN
store ON staff.staff_id = store.manager_staff_id
INNER JOIN
address ON store.address_id = address.address_id
INNER JOIN
city ON address.city_id = city.city_id
INNER JOIN
country ON city.country_id = country.country_id;
/*
2. I would like to get a better understanding of all of the inventory that would come along with the business.
Please pull together a list of each inventory item you have stocked, including the store_id number,
the inventory_id, the name of the film, the film’s rating, its rental rate and replacement cost.
*/
SELECT
inventory.store_id,
inventory.inventory_id,
film.title AS name_of_film,
film.rating,
film.rental_rate,
film.replacement_cost
FROM
inventory
INNER JOIN
film ON inventory.film_id = film.film_id;
/*
3. From the same list of films you just pulled, please roll that data up and provide a summary level overview
of your inventory. We would like to know how many inventory items you have with each rating at each store.
*/
SELECT
inventory.store_id,
film.rating,
COUNT(film.film_id) AS number_of_items
FROM
inventory
INNER JOIN
film ON inventory.film_id = film.film_id
GROUP BY inventory.store_id , film.rating
ORDER BY inventory.store_id;
/*
4. Similarly, we want to understand how diversified the inventory is in terms of replacement cost. We want to
see how big of a hit it would be if a certain category of film became unpopular at a certain store.
We would like to see the number of films, as well as the average replacement cost, and total replacement cost,
sliced by store and film category.
*/
SELECT
inventory.store_id,
category.name as category,
COUNT(film.film_id) AS number_of_films,
AVG(film.replacement_cost) AS average_replacement_cost,
SUM(film.replacement_cost) AS total_replacement_cost
FROM
film
INNER JOIN
film_category ON film.film_id = film_category.film_id
INNER JOIN
inventory ON film_category.film_id = inventory.film_id
INNER JOIN
category ON film_category.category_id = category.category_id
GROUP BY inventory.store_id , category.name;
/*
5. We want to make sure you folks have a good handle on who your customers are. Please provide a list
of all customer names, which store they go to, whether or not they are currently active,
and their full addresses – street address, city, and country.
*/
SELECT
customer.first_name,
customer.last_name,
address.address,
address.address2,
city.city,
country.country,
CASE
WHEN customer.active = 1 THEN 'active'
ELSE 'inactive'
END AS customer_status
FROM
customer
INNER JOIN
address ON customer.address_id = address.address_id
INNER JOIN
city ON address.city_id = city.city_id
INNER JOIN
country ON city.country_id = country.country_id
GROUP BY customer.customer_id;
/*
6. We would like to understand how much your customers are spending with you, and also to know
who your most valuable customers are. Please pull together a list of customer names, their total
lifetime rentals, and the sum of all payments you have collected from them. It would be great to
see this ordered on total lifetime value, with the most valuable customers at the top of the list.
*/
SELECT
customer.first_name,
customer.last_name,
COUNT(payment.rental_id) AS total_lifetime_rentals,
SUM(payment.amount) AS sum_of_all_payments
FROM
customer
LEFT JOIN
payment ON customer.customer_id = payment.customer_id
GROUP BY customer.customer_id
ORDER BY sum(payment.amount) DESC;
/*
7. My partner and I would like to get to know your board of advisors and any current investors.
Could you please provide a list of advisor and investor names in one table?
Could you please note whether they are an investor or an advisor, and for the investors,
it would be good to include which company they work with.
*/
SELECT
'advisor' AS type,
advisor.first_name,
advisor.last_name,
NULL AS company_name
FROM
advisor
UNION SELECT
'investor' AS type,
investor.first_name,
investor.last_name,
company_name
FROM
investor;
/*
8. We're interested in how well you have covered the most-awarded actors.
Of all the actors with three types of awards, for what % of them do we carry a film?
And how about for actors with two types of awards? Same questions.
Finally, how about actors with just one award?
*/
SELECT
CASE
WHEN actor_award.awards = 'Emmy, Oscar, Tony ' THEN 3
WHEN actor_award.awards = 'Emmy, Oscar' THEN 2
WHEN actor_award.awards = 'Emmy, Tony' THEN 2
WHEN actor_award.awards = 'Oscar, Tony' THEN 2
ELSE 1
END AS total_awards_per_actor,
AVG(CASE
WHEN actor_award.actor_id IS NOT NULL THEN 1
ELSE 0
END) AS percentage_of_awarded_actors_we_carry_film
FROM
actor_award
GROUP BY total_awards_per_actor;