-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_by_exercises.sql
More file actions
156 lines (137 loc) · 4.2 KB
/
group_by_exercises.sql
File metadata and controls
156 lines (137 loc) · 4.2 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
-- GROUP BY Exercises --
USE employees
;
SELECT *
FROM employees
LIMIT 10
;
-- In your script, use DISTINCT to find the unique titles in the titles table. How many unique titles have there ever been?
-- A: 7
SELECT DISTINCT title
FROM titles
;
-- Write a query to to find a list of all unique last names of all employees that start and end with 'E' using GROUP BY.
SELECT last_name
FROM employees
WHERE last_name LIKE 'e%e'
GROUP BY last_name
;
-- 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 first_name, last_name, COUNT(*)
FROM employees
WHERE last_name LIKE 'e%e'
GROUP BY last_name, first_name
ORDER BY first_name
;
-- 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.
-- A: Chleq, Lindqvist, Qiwen
SELECT last_name
FROM employees
WHERE last_name LIKE '%q%'
AND last_name NOT LIKE '%qu%'
GROUP BY last_name
;
-- Add a COUNT() to your results (the query above) to find the number of employees with the same last name.
SELECT last_name, COUNT(*)
FROM employees
WHERE last_name LIKE '%q%'
AND last_name NOT LIKE '%qu%'
GROUP BY last_name
;
-- Find all employees with first names 'Irena', 'Vidya', or 'Maya'. Use COUNT(*) and GROUP BY to find the number of employees for each gender with those names.
SELECT first_name, gender, COUNT(*)
FROM employees
WHERE first_name IN ('Irena', 'Vidya', 'Maya')
GROUP BY first_name, gender
ORDER BY first_name
;
-- Using your query that generates a username for all of the employees, generate a count employees for each unique username.
SELECT
lower(
concat(
substr(first_name, 1, 1),
substr(last_name, 1, 4),
'_',
lpad(MONTH(birth_date), 2, 0),
substr(YEAR(birth_date), -2)))
AS username,
COUNT(*)
FROM employees
GROUP BY username
;
-- From your previous query, are there any duplicate usernames? What is the higest number of times a username shows up?
-- A: There are many duplicate usernames. 6 Is the highest number of times a username shows up.
SELECT
lower(
concat(
substr(first_name, 1, 1),
substr(last_name, 1, 4),
'_',
lpad(MONTH(birth_date), 2, 0),
substr(YEAR(birth_date), -2)))
AS username,
COUNT(*)
FROM employees
GROUP BY username
ORDER BY COUNT(*) DESC
;
-- Bonus: How many duplicate usernames are there from your previous query?
-- 13251
SELECT COUNT(d.username)
FROM (SELECT
lower(
concat(
substr(first_name, 1, 1),
substr(last_name, 1, 4), '_',
lpad(MONTH(birth_date), 2, 0),
substr(YEAR(birth_date), -2))) AS username, COUNT(*) as count
FROM employees
GROUP BY username
HAVING count > 1) AS d
;
-- Bonus: More practice with aggregate functions --
-- 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, round(avg(salary), 2)
FROM salaries
WHERE to_date < now()
GROUP BY emp_no
;
-- 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.
DESCRIBE dept_emp;
SELECT dept_no, SUM(emp_no)
FROM dept_emp
WHERE to_date > now()
GROUP BY dept_no
;
-- Determine how many different salaries each employee has had. This includes both historic and current.
SELECT emp_no, COUNT(salary)
FROM salaries
GROUP BY emp_no
;
-- Find the maximum salary for each employee.
SELECT emp_no, max(salary)
FROM salaries
GROUP BY emp_no
;
-- Find the minimum salary for each employee.
SELECT emp_no, min(salary)
FROM salaries
GROUP BY emp_no
;
-- Find the standard deviation of salaries for each employee.
SELECT emp_no, STDDEV(salary)
FROM salaries
GROUP BY emp_no
;
-- Now find the max salary for each employee where that max salary is greater than $150,000.
SELECT emp_no, max(salary)
FROM salaries
GROUP BY emp_no
HAVING max(salary) > 150000
;
-- Find the average salary for each employee where that average salary is between $80k and $90k.
SELECT emp_no, round(avg(salary), 2)
FROM salaries
GROUP BY emp_no
HAVING avg(salary) BETWEEN 80000 AND 90000
;