-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_by_exercises.sql
More file actions
373 lines (308 loc) · 6.09 KB
/
group_by_exercises.sql
File metadata and controls
373 lines (308 loc) · 6.09 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* Question 1: Create a new file named group_by_exercises.sql */
/* Question 2.
In your script, use DISTINCT to find the unique titles in the titles table.
How many unique titles have there ever been? Answer that in a comment in your SQL file.
*/
USE employees;
SELECT
COUNT(distinct (title))
From
titles;
# There are 7 distinct titles in the title table.
/* Question 3.
Write a query to find a list of all unique last names that start and end with 'E'
using GROUP BY.
*/
SELECT
distinct(last_name)
FROM
employees
WHERE
last_name like 'e%e'
GROUP BY
last_name;
-- Insturctro versoin:
SELECT distinct
last_name
FROM
employees
HAVING last_name like '%e' and last_name like 'e%';
/* Question 4.
Write a query to to find all unique combinations of first and last names of
all employees whose last names start and end with 'E' */
SELECT distinct
first_name,
last_name
FROM
employees
WHERE
last_name like 'e%e';
-- instructor:
SELECT distinct
first_name
, last_name
From
employees
Having
last_name like 'e%e'
Order BY
last_name;
/* Question 5.
Write a query to find the unique last names with a 'q' but not 'qu'.
Include those names in a comment in your sql code. */
SELECT distinct
last_name
FROM
employees
WHERE
last_name like '%q%' and last_name not like '%qu%';
# Names: 'Chleq', 'Lindqvist', 'Qiwen'
-- instructor:
SELECT distinct
last_name
FROM
employees
Having
last_name like '%q%' and last_name not like '%qu%';
/* Question 6
Add a COUNT() to your results for exercise 5 to find the number of employees with the same last name.
*/
SELECT distinct
last_name,
count(*)
FROM
employees
WHERE
last_name like '%q%' and last_name not like '%qu%'
GROUP by
last_name;
-- Instructor:
SELECT distinct
last_name,
count(*)
FROM
employees
Group by
last_name
Having
last_name like '%q%' and last_name not like '%qu%';
/* Question 7.
Find all employees with first names 'Irena', 'Vidya', or 'Maya'.
Use COUNT(*) and GROUP BY to find the number of employees with those names for each gender.
*/
/* WRONG:
SELECT COUNT(
first_name)
FROM
employees
WHERE
first_name IN ('Irena', 'Vidya' , 'Maya');
*/
SELECT
first_name,
Count(*),
gender
FROM
employees
WHERE
first_name IN ('Irena', 'Vidya' , 'Maya')
GROUP BY
first_name,
gender;
/* Question 8
Using your query that generates a username for all employees, generate a count of employees with each unique username.
*/
SELECT distinct (
COUNT(Concat(
lower(LEFT(first_name, 1))
, lower(LEFT(last_name, 4))
, ' _ '
, DATE_FORMAT(birth_date, '%m%y')))) as username_count
FROM
employees;
-- Instructor:
SELECT
concat(
Lower(Substr(first_name, 1, 1))
,Lower(substr(last_name, 1, 4))
,' _ '
,substr(birth_date, 6, 2)
,substr(birth_date, 3, 2)) as username
, Count(*)
FROM
employees
Group by
username;
/* Question 9
From your previous query, are there any duplicate usernames?
What is the highest number of times a username shows up? Bonus: How many duplicate usernames are there?
*/
SELECT
COUNT(Concat(
lower(LEFT(first_name, 1))
, lower(LEFT(last_name, 4))
, ' _ '
, DATE_FORMAT(birth_date, '%m%y'))) as username_count
,Concat(
lower(LEFT(first_name, 1))
, lower(LEFT(last_name, 4))
, ' _ '
, DATE_FORMAT(birth_date, '%m%y')) as username
FROM
employees
GROUP BY
username
Having
(username_count) > 1
ORDER By
username_count DESC;
-- Instructor:
SELECT
concat(
Lower(Substr(first_name, 1, 1))
,Lower(substr(last_name, 1, 4))
,' _ '
,substr(birth_date, 6, 2)
,substr(birth_date, 3, 2)) as username
, Count(*)
FROM
employees
Group by
username
ORDER by
count(*) DESC;
# There are duplicate usernames and the hightest time a username shows up is 6 times.
SELECT
count(*)
FROM
(SELECT
Concat(
lower(LEFT(first_name, 1))
, lower(LEFT(last_name, 4))
, ' _ '
, DATE_FORMAT(birth_date, '%m%y')) as username
FROM
employees
Group by
username
Having
count(*) > 1
Order by
count(*) DESC) as bonus_question;
# There are 13251 duplicate usernames
/* Bonus Questions
B1. Determine the historic average salary for each employee.
When you hear, read, or think "for each" with regard to SQL, you'll probably be grouping by that exact column.
*/
SELECT
emp_no
, avg(salary) as historic_average
FROM
salaries
GROUP by
emp_no;
-- INRUCTOR
SELECT
avg(salary) as historic_average,
emp_no
FROM
salaries
GROUp by
emp_no;
/*
B2. Using the dept_emp table, count how many current employees work in each department.
The query result should show 9 rows, one for each department and the employee count.
*/
/* WRONG
SELECT
Count(emp_no) as number_of_employees
, dept_no
From
dept_emp
Group by
dept_no;
*/
-- INSTRUCTOR
SELECT
dept_no,
count(distinct emp_no) as num_current_employees
FROM
dept_emp
Where
to_date > now()
Group by
dept_no;
/*
3. Determine how many different salaries each employee has had.
This includes both historic and current.
*/
SELECT
emp_no
, count(distinct salary) as different_salary
FROM
salaries
GROUP by
emp_no;
-- Instructor:
SELECT
emp_no,
count(salary)
FROM
salaries
GROUP by
emp_no;
/*
5. Find the maximum salary for each employee.
*/
SELECT
MAX(salary) as miximum_salary,
emp_no
FROM
salaries
GROUP by
emp_no;
/*
6. Find the minimum salary for each employee.
*/
SELECT
MIN(salary) as minimum_salary,
emp_no
FROM
salaries
GROUP by
emp_no;
/*
7. Find the standard deviation of salaries for each employee.
*/
SELECT
round(stddev(salary), 1) as standard_deviation
, emp_no
, count(*)
FROM
salaries
GROUP by
emp_no;
/*
8. Find the max salary for each employee where that max salary is greater than $150,000
*/
SELECT
MAX(salary) as max_salary,
emp_no
FROM
salaries
GROUp by
emp_no
having
max_salary > 150000;
/*
9.Find the average salary for each employee where that average salary is between $80k and $90k.
*/
SELECT
AVG(salary) as avg_salary,
emp_no
FROM
salaries
GROUp by
emp_no
having
avg_salary between 80000 and 90000;