-
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 (56 loc) · 1.8 KB
/
group_by_exercises.sql
File metadata and controls
156 lines (56 loc) · 1.8 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
USE employees;
-- use DISTINCT to find the unique titles in the titles table.
SELECT DISTINCT title
FROM titles;
SELECT title
FROM titles
GROUP BY title;
-- query for employees whose last names start and end with 'E'.
-- Update the query find just the unique last names
SELECT last_name
FROM employees
WHERE last_name LIKE 'e%e'
GROUP BY last_name;
-- Update that query to find unique combinations
-- of first and last name
-- where the last name starts and ends with 'E'.
-- You should get 846 rows.
SELECT CONCAT(first_name, " ", last_name) as "full_name"
FROM employees
WHERE last_name LIKE 'e%e'
GROUP BY full_name;
-- Find the unique last names with a 'q' but not 'qu'
SELECT last_name
FROM employees
WHERE last_name like '%q%' AND NOT LIKE '%qu%'
GROUP BY last_name;
-- Add a COUNT() to your results and
-- use ORDER BY to make it easier to find employees
-- whose unusual name is shared with others.
SELECT last_name, count(*)
FROM employees
WHERE last_name like '%q%' AND last_name NOT LIKE '%qu%'
GROUP BY last_name;
-- Update your query for 'Irena', 'Vidya', or 'Maya'.
-- Use COUNT(*) and GROUP BY to find the number of employees
-- for each gender with those names. Your results should be:
-- 441 M
-- 268 F
SELECT gender, count(*)
FROM employees
WHERE first_name IN ("Irena", "Vidya", "Maya")
GROUP BY gender;
-- Recall the query from the last lesson
-- for generated usernames.
-- Are there any duplicate usernames?
SELECT
CONCAT(
LOWER(LEFT(first_name, 1)),
LOWER(LEFT(last_name, 4)),
'_',
LPAD(MONTH(birth_date), 2, '0'), # month
RIGHT(YEAR(birth_date), 2) # two digit year
) as username
FROM employees
GROUP BY username;
-- Bonus: how many duplicate usernames are there?