-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChinook Project.sql
More file actions
330 lines (271 loc) · 6.67 KB
/
Copy pathChinook Project.sql
File metadata and controls
330 lines (271 loc) · 6.67 KB
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
USE chinook;
SHOW TABLES;
# Question 1 Which countries generate the highest total revenue?
SELECT
c.Country, SUM(i.Total) AS TotalRevenue
FROM
Invoice i
JOIN
Customer c ON i.CustomerId = c.CustomerId
GROUP BY c.Country
ORDER BY TotalRevenue DESC;
# Question 2 Who are the customers with the highest lifetime value?
SELECT
c.CustomerId,
c.FirstName,
c.LastName,
SUM(i.Total) AS LifetimeValue
FROM
Customer c
JOIN
Invoice i ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId , c.FirstName , c.LastName
ORDER BY LifetimeValue DESC;
# Question 3 Which sales support employee manages the highest revenue customers?
SELECT
e.EmployeeId,
e.FirstName,
e.LastName,
SUM(i.Total) AS ManagedRevenue
FROM
Employee e
JOIN
Customer c ON e.EmployeeId = c.SupportRepId
JOIN
Invoice i ON c.CustomerId = i.CustomerId
GROUP BY e.EmployeeId , e.FirstName , e.LastName
ORDER BY ManagedRevenue DESC;
# Question 5 Identify the countries with the top sales.
SELECT
c.Country, SUM(i.Total) AS TotalSales
FROM
Invoice i
JOIN
Customer c ON i.CustomerId = c.CustomerId
GROUP BY c.Country
ORDER BY TotalSales DESC;
# Question 6 What is the customer distribution by country?
SELECT
Country, COUNT(*) AS NumberOfCustomers
FROM
Customer
GROUP BY Country
ORDER BY NumberOfCustomers DESC;
# Question 7 Average invoice total per country
SELECT
c.Country, AVG(i.Total) AS AverageInvoiceTotal
FROM
Invoice i
JOIN
Customer c ON i.CustomerId = c.CustomerId
GROUP BY c.Country
ORDER BY AverageInvoiceTotal DESC;
# Question 8 Tracks with the highest unit price
SELECT
TrackId, Name, UnitPrice
FROM
Track
ORDER BY UnitPrice DESC;
# Question 9 Retrieve all tracks that are longer than 5 minutes.
SELECT
TrackId, Name, Milliseconds
FROM
Track
WHERE
Milliseconds > 300000
ORDER BY Milliseconds DESC;
# Question 10 Show all customers who are from the USA
SELECT
*
FROM
Customer
WHERE
Country = 'USA';
# Question 11 Retrieve all invoices where the total amount is greater than 10.
SELECT
*
FROM
Invoice
WHERE
Total > 10;
# Question 12 What are the most popular music genres by number of tracks sold?
SELECT
g.Name AS Genre, SUM(il.Quantity) AS UnitsSold
FROM
Genre g
JOIN
Track t ON g.GenreId = t.GenreId
JOIN
InvoiceLine il ON t.TrackId = il.TrackId
GROUP BY g.Name
ORDER BY UnitsSold DESC;
# Question 13 Which tracks generate the most revenue?
SELECT
t.TrackId,
t.Name,
SUM(il.UnitPrice * il.Quantity) AS Revenue
FROM
Track t
JOIN
InvoiceLine il ON t.TrackId = il.TrackId
GROUP BY t.TrackId , t.Name
ORDER BY Revenue DESC;
# Question 14 Which albums sell the most tracks?
SELECT
a.Title, COUNT(il.InvoiceLineId) AS TracksSold
FROM
Album a
JOIN
Track t ON a.AlbumId = t.AlbumId
JOIN
InvoiceLine il ON t.TrackId = il.TrackId
GROUP BY a.Title
ORDER BY TracksSold DESC;
# Question 15 Which artists generate the most revenue?
SELECT
ar.name as Artist, SUM(il.Unitprice * il.Quantity) AS Revenue
FROM
artist ar
JOIN
album a ON ar.artistid = a.ArtistId
JOIN
track t ON t.AlbumId = a.AlbumId
JOIN
invoiceline il ON t.TrackId = il.TrackId
GROUP BY Artist
ORDER BY Revenue DESC;
# Qestion 16 What is the average invoice value per country?
SELECT
billingcountry AS country,
ROUND(AVG(total), 2) AS Average_Invoice_Value
FROM
invoice
GROUP BY country
ORDER BY Average_Invoice_Value DESC;
# Question 17 What is the average track duration by genre?
SELECT
g.Name AS Genre,
ROUND(AVG(t.Milliseconds), 2) AS AvgDurationMs
FROM
Genre g
JOIN
Track t ON g.GenreId = t.GenreId
GROUP BY g.Name
ORDER BY AvgDurationMs DESC;
# Question 18 Which playlists contain the most tracks?
SELECT
p.Name, COUNT(pt.TrackId) AS NumberOfTracks
FROM
Playlist p
JOIN
PlaylistTrack pt ON p.PlaylistId = pt.PlaylistId
GROUP BY p.Name
ORDER BY NumberOfTracks DESC;
# Question 19 Which media type generates the highest revenue?
SELECT
m.Name AS MediaType,
SUM(il.UnitPrice * il.Quantity) AS Revenue
FROM
MediaType m
JOIN
Track t ON m.MediaTypeId = t.MediaTypeId
JOIN
InvoiceLine il ON t.TrackId = il.TrackId
GROUP BY m.Name
ORDER BY Revenue DESC;
# Question 20 Which customers made the most purchases?
SELECT
c.CustomerId,
c.FirstName,
c.LastName,
COUNT(i.InvoiceId) AS NumberOfPurchases
FROM
Customer c
JOIN
Invoice i ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId , c.FirstName , c.LastName
ORDER BY NumberOfPurchases DESC;
# Question 21 What are the top-selling tracks per genre?
SELECT
g.Name AS Genre,
t.Name AS Track,
SUM(il.Quantity) AS UnitsSold
FROM
Genre g
JOIN
Track t ON g.GenreId = t.GenreId
JOIN
InvoiceLine il ON t.TrackId = il.TrackId
GROUP BY g.Name , t.Name
ORDER BY g.Name , UnitsSold DESC;
# Question 22 Which cities have the most customers?
SELECT
City, COUNT(*) AS NumberOfCustomers
FROM
Customer
GROUP BY City
ORDER BY NumberOfCustomers DESC;
# Question 23 What are the most expensive tracks?
SELECT
Name, UnitPrice
FROM
Track
ORDER BY UnitPrice DESC;
# Question 24 Which employees report to which managers? (hierarchy query)
SELECT e.FirstName AS Employee,
m.FirstName AS Manager
FROM Employee e
LEFT JOIN Employee m ON e.ReportsTo = m.EmployeeId;
# Question 25 Find tracks longer than the average length
SELECT
*
FROM
Track
WHERE
Milliseconds > (SELECT
AVG(Milliseconds)
FROM
Track);
# Question 26 Revenue trend by invoice date
SELECT
InvoiceDate, SUM(Total) AS DailyRevenue
FROM
Invoice
GROUP BY InvoiceDate
ORDER BY InvoiceDate;
# Question 27 Which customers haven’t made any purchases?
SELECT
c.*
FROM
Customer c
LEFT JOIN
Invoice i ON c.CustomerId = i.CustomerId
WHERE
i.InvoiceId IS NULL;
# Question 28 Which tracks appear in the most playlists?
SELECT
t.Name, COUNT(pt.PlaylistId) AS PlaylistCount
FROM
Track t
JOIN
PlaylistTrack pt ON t.TrackId = pt.TrackId
GROUP BY t.Name
ORDER BY PlaylistCount DESC;
# Question 29 Total revenue per year (sales performance)
SELECT YEAR(InvoiceDate) AS Year,
SUM(Total) AS Revenue
FROM Invoice
GROUP BY Year
ORDER BY Year;
# Question 30 Who are the top customers in each country?
SELECT
c.Country,
c.FirstName,
c.LastName,
SUM(i.Total) AS TotalSpent
FROM
Customer c
JOIN
Invoice i ON c.CustomerId = i.CustomerId
GROUP BY c.Country , c.FirstName , c.LastName
ORDER BY c.Country , TotalSpent DESC;