This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chinook.sql
228 lines (202 loc) · 6.79 KB
/
chinook.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
-- 1. Provide a query showing Customers (just their full names, customer ID and country) who are not in the US.
SELECT
FirstName || " " || LastName AS Name,
CustomerId,
Country
FROM Customer
WHERE Country <> "USA";
-- 2. Provide a query only showing the Customers from Brazil.
SELECT * FROM Customer
WHERE Country IS "Brazil";
-- 3. Provide a query showing the Invoices of customers who are from Brazil. The resultant table should show the customer's full name, Invoice ID, Date of the invoice and billing country.
SELECT
c.FirstName || " " || c.LastName as Name,
i.InvoiceId,
i.InvoiceDate,
c.Country
FROM Customer c
LEFT JOIN Invoice i ON i.CustomerId = c.CustomerId
WHERE c.Country IS "Brazil";
-- 4. Provide a query showing only the Employees who are Sales Agents.
SELECT * FROM Employee
WHERE Title LIKE "%Sale%Agent%";
-- 5. Provide a query showing a unique list of billing countries from the Invoice table.
SELECT BillingCountry FROM Invoice GROUP BY BillingCountry;
-- 6. Provide a query that shows the invoices associated with each sales agent. The resultant table should include the Sales Agent's full name.
SELECT
i.InvoiceId,
(e.FirstName || " " || e.LastName) as Name
FROM Invoice i
LEFT JOIN Customer c ON c.CustomerId = i.CustomerId
LEFT JOIN Employee e ON c.SupportRepId = e.EmployeeId;
-- 7. Provide a query that shows the Invoice Total, Customer name, Country and Sale Agent name for all invoices and customers.
SELECT
(c.FirstName || " " || c.LastName) as CustomerName,
(e.FirstName || " " || e.LastName) as SalesAgentName,
i.Total,
i.BillingCountry
FROM Invoice i
LEFT JOIN Customer c ON c.CustomerId = i.CustomerId
LEFT JOIN Employee e ON c.SupportRepId = e.EmployeeId;
-- 8. How many Invoices were there in 2009 and 2011? What are the respective total sales for each of those years?
SELECT
(SELECT
COUNT(*)
FROM Invoice i
WHERE SUBSTR(i.InvoiceDate,0,5) = "2009") AS Invoices09,
(SELECT
COUNT(*)
FROM Invoice i
WHERE SUBSTR(i.InvoiceDate,0,5) = "2011") AS Invoices11;
-- 9. Looking at the InvoiceLine table, provide a query that COUNTs the number of line items for Invoice ID 37.
SELECT
COUNT(*)
FROM InvoiceLine il WHERE il.InvoiceId = 37;
-- 10. Looking at the InvoiceLine table, provide a query that COUNTs the number of line items for each Invoice.
SELECT
InvoiceId,
COUNT(*)
FROM InvoiceLine il
GROUP BY InvoiceId;
-- 11. Provide a query that includes the track name with each invoice line item.
SELECT
t.Name,
il.*
FROM InvoiceLine il
LEFT JOIN Track t ON t.TrackId = il.TrackId;
-- 12. Provide a query that includes the purchased track name AND artist name with each invoice line item.
SELECT
t.Name as Song,
ar.Name AS Artist,
il.* FROM InvoiceLine il
LEFT JOIN Track t ON t.TrackId = il.TrackId
LEFT JOIN Album al ON al.AlbumId = t.AlbumId
LEFT JOIN Artist ar ON ar.ArtistId = al.ArtistId;
-- 13. Provide a query that shows the # of invoices per country.
SELECT
Invoice.BillingCountry,
COUNT(*)
FROM Invoice
GROUP BY BillingCountry;
-- 14. Provide a query that shows the total number of tracks in each playlist. The Playlist name should be included on the resultant table.
SELECT
p.Name,
COUNT(*)
FROM Playlist p
LEFT JOIN PlaylistTrack pt ON pt.PlaylistId = p.PlaylistId
GROUP BY pt.PlaylistId;
-- 15. Provide a query that shows all the Tracks, but displays no IDs. The resultant table should include the Album name, Media type and Genre.
SELECT
t.Name AS Song,
a.Title as Album,
mt.Name AS MediaType,
g.Name AS Genre
FROM Track t
LEFT JOIN MediaType mt on mt.MediaTypeId = t.MediaTypeId
LEFT JOIN Album a ON a.AlbumId = t.AlbumId
LEFT JOIN Genre g ON g.GenreId = t.GenreId;
-- 16. Provide a query that shows all Invoices but includes the # of invoice line items.
SELECT
i.*,
COUNT(*) as LineCount
FROM Invoice i
LEFT JOIN InvoiceLine il ON i.InvoiceId = il.InvoiceId
GROUP BY i.InvoiceId;
-- 17. Provide a query that shows total sales made by each sales agent.
SELECT
e.FirstName || " " || e.lastName as Name,
COUNT(*) AS Sales
FROM Employee e
JOIN Customer c ON c.SupportRepId = e.EmployeeId
JOIN Invoice i ON i.CustomerId = c.CustomerId
GROUP BY EmployeeId;
-- 18. Which sales agent made the most in sales in 2009?
-- Margaret Park
SELECT
e.FirstName || " " || e.lastName as Name,
COUNT(*) AS Sales
FROM Employee e
JOIN Customer c ON c.SupportRepId = e.EmployeeId
JOIN Invoice i ON i.CustomerId = c.CustomerId
WHERE SUBSTR(i.InvoiceDate,0,5) = "2009"
GROUP BY EmployeeId
ORDER BY Sales DESC;
-- 19. Which sales agent made the most in sales in 2010?
-- Jane Peacock
SELECT
e.FirstName || " " || e.lastName as Name,
COUNT(*) AS Sales
FROM Employee e
JOIN Customer c ON c.SupportRepId = e.EmployeeId
JOIN Invoice i ON i.CustomerId = c.CustomerId
WHERE SUBSTR(i.InvoiceDate,0,5) = "2010"
GROUP BY EmployeeId
ORDER BY Sales DESC;
-- 20. Which sales agent made the most in sales over all?
-- Jane Peacock
SELECT
e.FirstName || " " || e.lastName as Name,
COUNT(*) AS Sales
FROM Employee e
JOIN Customer c ON c.SupportRepId = e.EmployeeId
JOIN Invoice i ON i.CustomerId = c.CustomerId
GROUP BY EmployeeId
ORDER BY Sales DESC;
-- 21. Provide a query that shows the # of customers assigned to each sales agent.
SELECT
e.EmployeeId,
e.FirstName || " " || e.LastName as Employee,
COUNT(*)
FROM Customer c
JOIN Employee e ON e.EmployeeId = c.SupportRepId
GROUP BY e.EmployeeId;
-- 22. Provide a query that shows the total sales per country. Which country's customers spent the most?
-- USA
SELECT
BillingCountry,
SUM(Total)
FROM Invoice
GROUP BY BillingCountry
ORDER BY SUM(Total) DESC;
-- 23. Provide a query that shows the most purchased track of 2013.
-- All track were purchased no more than once.
SELECT
t.TrackId,
COUNT(*)
FROM InvoiceLine il
JOIN Track t ON t.TrackId = il.TrackId
JOIN Invoice i ON i.InvoiceId = il.InvoiceId
WHERE SUBSTR(i.InvoiceDate,0,5) = "2013"
GROUP BY il.TrackId
ORDER BY COUNT(*) DESC;
-- 24. Provide a query that shows the top 5 most purchased tracks over all.
-- There are at least 50 tied for first place.
SELECT
t.TrackId,
COUNT(*)
FROM InvoiceLine il
JOIN Track t ON t.TrackId = il.TrackId
GROUP BY il.TrackId
ORDER BY COUNT(*) DESC;
-- 25. Provide a query that shows the top 3 best selling artists.
SELECT
ar.Name,
COUNT(*)
FROM InvoiceLine il
LEFT JOIN Track t ON t.TrackId = il.TrackId
LEFT JOIN Album al ON al.AlbumId = t.AlbumId
LEFT JOIN Artist ar ON ar.ArtistId = al.ArtistId
GROUP BY ar.ArtistId
ORDER BY COUNT(*) DESC
LIMIT 3;
-- 26. Provide a query that shows the most purchased Media Type.
-- MPEG
SELECT
mt.Name,
COUNT(*)
FROM InvoiceLine il
LEFT JOIN Track t ON t.TrackId = il.TrackId
LEFT JOIN MediaType mt ON mt.MediaTypeId = t.MediaTypeId
GROUP BY t.MediaTypeId
ORDER BY COUNT(*) DESC;
-- 27. Provide a query that shows the number tracks purchased in all invoices that contain more than one genre.