-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathModule3_CodingAssignment
283 lines (207 loc) · 8.45 KB
/
Module3_CodingAssignment
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
All of the questions in this quiz refer to the open source Chinook Database.
Please familiarize yourself with the ER diagram in order to familiarize yourself
with the table and column names in order to write accurate queries and get the appropriate answers.
ER Diagram:
https://ucde-rey.s3.amazonaws.com/DSV1015/ChinookDatabaseSchema.png
------------------------------------------------------------------------------------
#_1:
Q:
Using a subquery,
find the names of all the tracks for the album "Californication".
What is the title of the 8th track?
A:
Porcelain
SQL Query:
SELECT Name, AlbumID
FROM Tracks
WHERE AlbumId IN ( SELECT AlbumId
FROM Albums
WHERE Title='Californication'
);
SQL Output:
+-------------------+---------+
| Name | AlbumId |
+-------------------+---------+
| Around The World | 195 |
| Parallel Universe | 195 |
| Scar Tissue | 195 |
| Otherside | 195 |
| Get On Top | 195 |
| Californication | 195 |
| Easily | 195 |
| Porcelain | 195 |
| Emit Remmus | 195 |
| I Like Dirt | 195 |
+-------------------+---------+
(Output limit exceeded, 10 of 15 total rows shown)
------------------------------------------------------------------------------------
#_2:
Q:
Find the total number of invoices for each customer
along with the customer's full name, city and email.
After running the query described above,
what is the email address of the 5th person,
František Wichterlová?
Enter the answer below.
A:
SQL Query:
SELECT FirstName, LastName, City, Email, COUNT(I.CustomerId) AS InvoiceCount
FROM Customers C
INNER JOIN Invoices I
ON C.CustomerId = I.CustomerId
GROUP BY C.CustomerId
SQL Output:
+-----------+-------------+---------------------+--------------------------+--------------+
| FirstName | LastName | City | Email | InvoiceCount |
+-----------+-------------+---------------------+--------------------------+--------------+
| Luís | Gonçalves | São José dos Campos | [email protected] | 7 |
| Leonie | Köhler | Stuttgart | [email protected] | 7 |
| François | Tremblay | Montréal | [email protected] | 7 |
| Bjørn | Hansen | Oslo | [email protected] | 7 |
| František | Wichterlová | Prague | [email protected] | 7 |
| Helena | Holý | Prague | [email protected] | 7 |
| Astrid | Gruber | Vienne | [email protected] | 7 |
| Daan | Peeters | Brussels | [email protected] | 7 |
| Kara | Nielsen | Copenhagen | [email protected] | 7 |
| Eduardo | Martins | São Paulo | [email protected] | 7 |
+-----------+-------------+---------------------+--------------------------+--------------+
(Output limit exceeded, 10 of 59 total rows shown)
------------------------------------------------------------------------------------
Q_3:
Q:
Retrieve the track name, album, artistID, and trackID for all the albums.
What is the song title of trackID 12
from the "For Those About to Rock We Salute You" album? Enter the answer below.
A:
Breaking The Rules
SQL Query:
SELECT Tracks.Name, A.Name AS Artist, Albums.Title AS Album, Tracks.TrackId
FROM( (Tracks INNER JOIN Albums
ON Tracks.AlbumId = Albums.AlbumId)
INNER JOIN Artists A
ON A.ArtistId = Albums.ArtistId);
SQL Output:
+-----------------------------------------+--------+---------------------------------------+---------+
| Name | Artist | Album | TrackId |
+-----------------------------------------+--------+---------------------------------------+---------+
| For Those About To Rock (We Salute You) | AC/DC | For Those About To Rock We Salute You | 1 |
| Put The Finger On You | AC/DC | For Those About To Rock We Salute You | 6 |
| Let's Get It Up | AC/DC | For Those About To Rock We Salute You | 7 |
| Inject The Venom | AC/DC | For Those About To Rock We Salute You | 8 |
| Snowballed | AC/DC | For Those About To Rock We Salute You | 9 |
| Evil Walks | AC/DC | For Those About To Rock We Salute You | 10 |
| C.O.D. | AC/DC | For Those About To Rock We Salute You | 11 |
| Breaking The Rules | AC/DC | For Those About To Rock We Salute You | 12 |
| Night Of The Long Knives | AC/DC | For Those About To Rock We Salute You | 13 |
| Spellbound | AC/DC | For Those About To Rock We Salute You | 14 |
+-----------------------------------------+--------+---------------------------------------+---------+
(Output limit exceeded, 10 of 3503 total rows shown)
------------------------------------------------------------------------------------
#_4:
Q:
Retrieve a list
with the managers last name, and the last name of the employees
who report to him or her.
A:
Callahan
King
SQL Query:
SELECT M.LastName AS Manager,
E.LastName AS Employee
FROM Employees E
INNER JOIN Employees M
ON E.ReportsTo = M.EmployeeID
SQL Output:
+----------+----------+
| Manager | Employee |
+----------+----------+
| Adams | Edwards |
| Edwards | Peacock |
| Edwards | Park |
| Edwards | Johnson |
| Adams | Mitchell |
| Mitchell | King |
| Mitchell | Callahan |
+----------+----------+
------------------------------------------------------------------------------------
#_5:
Q:
Find the name and ID of the artists who do not have albums.
After running the query described above,
two of the records returned have the same last name.
Enter that name below.
A:
Gilberto
SQL Query:
SELECT Name AS Artist
,Artists.ArtistId
,Albums.Title AS Album
FROM Artists
LEFT JOIN Albums
ON Artists.ArtistId = Albums.ArtistId
WHERE Album IS NULL
SQL Output:
+----------------------------+----------+-------+
| Artist | ArtistId | Album |
+----------------------------+----------+-------+
| Milton Nascimento & Bebeto | 25 | None |
| Azymuth | 26 | None |
| João Gilberto | 28 | None |
| Bebel Gilberto | 29 | None |
| Jorge Vercilo | 30 | None |
| Baby Consuelo | 31 | None |
| Ney Matogrosso | 32 | None |
| Luiz Melodia | 33 | None |
| Nando Reis | 34 | None |
| Pedro Luís & A Parede | 35 | None |
+----------------------------+----------+-------+
(Output limit exceeded, 10 of 71 total rows shown)
------------------------------------------------------------------------------------
#_6:
Q:
Use a UNION to create a list of all the employee's and customer's first names and last names
ordered by the last name in descending order.
A:
Taylor
SQL Query:
SELECT FirstName, LastName
FROM Employees
UNION
SELECT FirstName, LastName
FROM Customers
ORDER BY LastName DESC
LIMIT 6
SQL Output:
+-----------+--------------+
| FirstName | LastName |
+-----------+--------------+
| Fynn | Zimmermann |
| Stanisław | Wójcik |
| František | Wichterlová |
| Johannes | Van der Berg |
| François | Tremblay |
| Mark | Taylor |
+-----------+--------------+
------------------------------------------------------------------------------------
#_7:
Q:
See if there are any customers
who have a different city listed in their billing city versus their customer city.
A:
0
SQL Query:
SELECT C.FirstName,
C.LastName,
C.City AS CustomerCity,
I.BillingCity
FROM Customers C
INNER JOIN Invoices I
ON C.CustomerId = I.CustomerId
WHERE CustomerCity != BillingCity
SQL Output:
+-----------+----------+----------+----------+
| FirstName | LastName | cstmCity | billCity |
+-----------+----------+----------+----------+
+-----------+----------+----------+----------+
(Zero rows)