forked from brianchiang-tw/SQL_for_DataScience
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule4_CodingAssignment
336 lines (220 loc) · 7.22 KB
/
Module4_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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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:
Pull a list of customer ids with the customer’s full name, and address,
along with combining their city and country together.
Be sure to make a space in between these two and make it UPPER CASE.
Q:
MOUNTAIN VIEW USA
SQL Query:
SELECT CustomerId
, FirstName || " " || LastName AS FullName
, Address
, UPPER( City || " " || Country ) AS CityCountry
FROM Customers
WHERE CustomerId=16
SQL Output:
MOUNTAIN VIEW USA
------------------------------------------------------------------------------------
#_2:
Q:
Create a new employee user id by combining
the first 4 letters of the employee’s first name with
the first 2 letters of the employee’s last name.
Make the new field lower case and pull each individual step
to show your work.
What is the final result for Robert King?
A:
robeki
SQL Query:
SELECT FirstName
, LastName
, LOWER( SUBSTR(FirstName, 1, 4) ) AS First_4prefix
, LOWER( SUBSTR(LastName, 1, 2) ) AS Last_2prefix
, LOWER( SUBSTR(FirstName, 1, 4) ) || LOWER( SUBSTR(LastName, 1, 2) ) AS UserID
FROM Employees
SQL Output:
+-----------+----------+---------------+--------------+--------+
| FirstName | LastName | First_4prefix | Last_2prefix | UserID |
+-----------+----------+---------------+--------------+--------+
| Andrew | Adams | andr | ad | andrad |
| Nancy | Edwards | nanc | ed | nanced |
| Jane | Peacock | jane | pe | janepe |
| Margaret | Park | marg | pa | margpa |
| Steve | Johnson | stev | jo | stevjo |
| Michael | Mitchell | mich | mi | michmi |
| Robert | King | robe | ki | robeki |
| Laura | Callahan | laur | ca | laurca |
+-----------+----------+---------------+--------------+--------+
------------------------------------------------------------------------------------
#_3:
Q:
Show a list of employees who have worked for the company for 15 or more years
using the current date function. Sort by lastname ascending.
What is the lastname of the last person on the list returned?
A:
SQL Query:
SELECT FirstName
, LastName
, HireDate
, (STRFTIME('%Y', 'now' ) - STRFTIME('%Y', HireDate) )
- (STRFTIME('%m-%d', 'now') < STRFTIME('%m-%d', HireDate) )
AS WorkYear
FROM Employees
WHERE WorkYear >= 15
ORDER BY LastName ASC
SQL Output:
+-----------+----------+---------------------+----------+
| FirstName | LastName | HireDate | WorkYear |
+-----------+----------+---------------------+----------+
| Andrew | Adams | 2002-08-14 00:00:00 | 16 |
| Laura | Callahan | 2004-03-04 00:00:00 | 15 |
| Nancy | Edwards | 2002-05-01 00:00:00 | 17 |
| Steve | Johnson | 2003-10-17 00:00:00 | 15 |
| Robert | King | 2004-01-02 00:00:00 | 15 |
| Michael | Mitchell | 2003-10-17 00:00:00 | 15 |
| Margaret | Park | 2003-05-03 00:00:00 | 16 |
| Jane | Peacock | 2002-04-01 00:00:00 | 17 |
+-----------+----------+---------------------+----------+
------------------------------------------------------------------------------------
#_4:
Q:
Profiling the Customers table, answer the following question.
A:
Phone, Fax, Company, Postal Code
SQL Query:
SELECT COUNT(*)
FROM Customers
WHERE Phone IS NULL
SQL Output:
+-----------+
| NullCount |
+-----------+
| 1 |
+-----------+
=========================
SQL Query:
SELECT COUNT(*)
FROM Customers
WHERE Fax IS NULL
SQL Output:
+-----------+
| NullCount |
+-----------+
| 47 |
+-----------+
=========================
SQL Query:
SELECT COUNT(*)
FROM Customers
WHERE FirstName IS NULL
SQL Output:
+-----------+
| NullCount |
+-----------+
| 0 |
+-----------+
=========================
SQL Query:
SELECT COUNT(*)
FROM Customers
WHERE Address IS NULL
SQL Output:
+-----------+
| NullCount |
+-----------+
| 0 |
+-----------+
=========================
SQL Query:
SELECT COUNT(*)
FROM Customers
WHERE Company IS NULL
SQL Output:
+-----------+
| NullCount |
+-----------+
| 49 |
+-----------+
=========================
SQL Query:
SELECT COUNT(*)
FROM Customers
WHERE PostalCode IS NULL
SQL Output:
+-----------+
| NullCount |
+-----------+
| 4 |
+-----------+
=========================
column: FirstName, PostalCode, Company, Fax, Phone, Address
NullCount: 0 4 49 47 1 0
Column with NullCount > 0: Postal Code, Company, Fax, Phone
------------------------------------------------------------------------------------
#-5:
Q:
Find the cities with the most customers and rank in descending order.
Which of the following cities indicate having 2 customers?
A:
Mountain View
Sao Paulo
London
SQL Query:
SELECT Customers.City
, COUNT(*) AS CustomerCount
FROM Customers
GROUP BY City
HAVING CustomerCount=2
ORDER BY CustomerCount DESC
SQL Output:
+---------------+---------------+
| City | CustomerCount |
+---------------+---------------+
| Berlin | 2 |
| London | 2 |
| Mountain View | 2 |
| Paris | 2 |
| Prague | 2 |
| São Paulo | 2 |
+---------------+---------------+
------------------------------------------------------------------------------------
#_6:
Q:
Create a new customer invoice id
by combining a customer’s invoice id with their first and last name
while ordering your query in the following order:
firstname, lastname, and invoiceID.
Select all of the correct "AstridGruber" entries
that are returned in your results below.
Select all that apply.
A:
AstridGruber273
AstridGruber296
AstridGruber370
SQL Query:
SELECT C.FirstName
, C.LastName
, I.invoiceId
, C.FirstName || C.LastName || I.InvoiceID AS newcID
FROM Customers C
INNER JOIN Invoices I
ON C.CustomerId = I.CustomerID
WHERE newcID LIKE 'AstridGruber%'
SQL Output:
+-----------+----------+-----------+-----------------+
| FirstName | LastName | InvoiceId | newcID |
+-----------+----------+-----------+-----------------+
| Astrid | Gruber | 78 | AstridGruber78 |
| Astrid | Gruber | 89 | AstridGruber89 |
| Astrid | Gruber | 144 | AstridGruber144 |
| Astrid | Gruber | 273 | AstridGruber273 |
| Astrid | Gruber | 296 | AstridGruber296 |
| Astrid | Gruber | 318 | AstridGruber318 |
| Astrid | Gruber | 370 | AstridGruber370 |
+-----------+----------+-----------+-----------------+