-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek3_practiseQuiz.sql
More file actions
210 lines (168 loc) · 5.69 KB
/
week3_practiseQuiz.sql
File metadata and controls
210 lines (168 loc) · 5.69 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
---Practice Quiz - Writing Queries---
/*
All of the questions in this quiz pull from the open source Chinook Database. Please refer to the ER Diagram below and familiarize yourself with the table and column names to write accurate queries and get the appropriate answers.
*/
/*
Q1. How many albums does the artist Led Zeppelin have?
*/
SELECT COUNT(DISTINCT(albums.AlbumId)), artists.Name
FROM artists INNER JOIN albums
ON artists. ArtistId = albums.ArtistId
WHERE Name = 'Led Zeppelin'
/*
+---------------------------------+--------------+
| count(distinct(albums.AlbumId)) | Name |
+---------------------------------+--------------+
| 14 | Led Zeppelin |
+---------------------------------+--------------+
Ans: 14
*/
/*
Q2. Create a list of album titles and the unit prices for the artist "Audioslave".
*/
SELECT t.UnitPrice, a.Title, ar.Name
FROM tracks t INNER JOIN albums a ON t.AlbumId = a.AlbumId
INNER JOIN artists ar ON ar.ArtistId = a.ArtistId
WHERE ar.Name = 'Audioslave'
/*
+-----------+--------------+------------+
| UnitPrice | Title | Name |
+-----------+--------------+------------+
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Audioslave | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
| 0.99 | Out Of Exile | Audioslave |
+-----------+--------------+------------+
(Output limit exceeded, 25 of 40 total rows shown)
How many records are returned?
Only 25 records will be shown in the output so please look at the bottom of the output to see how many records were retrieved.
Ans: 40
*/
/*
Q3. Find the first and last name of any customer who does not have an invoice. Are there any customers returned from the query?
*/
SELECT DISTINCT(i.InvoiceId), c.FirstName, c.LastName
FROM invoices i INNER JOIN customers c
ON c.CustomerId = i.CustomerId
WHERE i.InvoiceId IS NULL
/*
+-----------+-----------+----------+
| InvoiceId | FirstName | LastName |
+-----------+-----------+----------+
+-----------+-----------+----------+
(Zero rows)
Ans: No
*/
/*
Q4. Find the total price for each album.
*/
SELECT sum(t.UnitPrice), a.AlbumId
FROM tracks t INNER JOIN albums a ON a.AlbumId = t.AlbumId
GROUP BY a.AlbumId
/*
+------------------+---------+
| sum(t.UnitPrice) | AlbumId |
+------------------+---------+
| 9.9 | 1 |
| 0.99 | 2 |
| 2.97 | 3 |
| 7.92 | 4 |
| 14.85 | 5 |
| 12.87 | 6 |
| 11.88 | 7 |
| 13.86 | 8 |
| 7.92 | 9 |
| 13.86 | 10 |
| 11.88 | 11 |
| 11.88 | 12 |
| 7.92 | 13 |
| 12.87 | 14 |
| 4.95 | 15 |
| 6.93 | 16 |
| 9.9 | 17 |
| 16.83 | 18 |
| 10.89 | 19 |
| 10.89 | 20 |
| 17.82 | 21 |
| 2.97 | 22 |
| 33.66 | 23 |
| 22.77 | 24 |
| 12.87 | 25 |
+------------------+---------+
(Output limit exceeded, 25 of 347 total rows shown)
*/
SELECT sum(t.UnitPrice), a.AlbumId
FROM tracks t INNER JOIN albums a ON a.AlbumId = t.AlbumId
WHERE a.Title = 'Big Ones'
/*
+------------------+---------+
| sum(t.UnitPrice) | AlbumId |
+------------------+---------+
| 14.85 | 5 |
+------------------+---------+
What is the total price for the album “Big Ones”?
Ans: 14.85
*/
/*
Q5. How many records are created when you apply a Cartesian join to the invoice and invoice items table?
*/
SELECT i.InvoiceId, ii.InvoiceId
FROM invoices i CROSS JOIN invoice_items ii
/*
+-----------+------------+
| InvoiceId | InvoiceId |
+-----------+------------+
| 1 | 1 |
| 1 | 1 |
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 1 | 2 |
| 1 | 3 |
| 1 | 3 |
| 1 | 3 |
| 1 | 3 |
| 1 | 3 |
| 1 | 3 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 4 |
| 1 | 5 |
| 1 | 5 |
| 1 | 5 |
| 1 | 5 |
+-----------+------------+
(Output limit exceeded, 25 of 922880 total rows shown)
Only 25 records will be shown in the output so please look at the bottom of the output to see how many records were retrieved.
Ans: 922880
*/
/**/
/**/
/**/