-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin_exercises.sql
More file actions
279 lines (209 loc) · 8.54 KB
/
join_exercises.sql
File metadata and controls
279 lines (209 loc) · 8.54 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
#JOin example database
-- 1. Use the join_example_db. Select all the records from both the users and roles tables.
USE join_example_db;
SELECT * FROM users;
SELECT * FROM roles;
-- 2. Use join, left join, and right join to combine results from the users and roles tables as we did in the lesson. Before you run each query, guess the expected number of results.
SELECT * FROM users LEFT JOIN roles ON users.role_id = roles.id;
SELECT * FROM roles LEFT JOIN users ON users.role_id = roles.id;
-- 3. Although not explicitly covered in the lesson, aggregate functions like count can be used with join queries. Use count and the appropriate join type to get a list of roles along with the number of users that has the role. Hint: You will also need to use group by in the query.
SELECT roles.name, COUNT FROM users JOIN roles ON users.role_id = roles.id GROUP BY roles.name;
-- 1. Use the employees database.
USE employees;
-- 2. Using the example in the Associative Table Joins section as a guide, write a query that shows each department along with the name of the current manager for that department.
SELECT * FROM dept_manager WHERE to_date = '9999-01-01';
SELECT dept_name AS 'Department Name' ,
CONCAT(first_name,' ',last_name) AS 'Department Manager'
FROM dept_manager AS D
JOIN employees AS E
ON D.emp_no = E.emp_no
JOIN departments AS DEP
ON DEP.dept_no = D.dept_no
WHERE to_date = '9999-01-01' ORDER BY dept_name ASC;
-- 3. Find the name of all departments currently managed by women.
SELECT dept_name AS 'Department Name' ,
CONCAT(first_name,' ',last_name) AS 'Department Manager',
gender
FROM dept_manager AS D
JOIN employees AS E
ON D.emp_no = E.emp_no
JOIN departments AS DEP
ON DEP.dept_no = D.dept_no
WHERE to_date = '9999-01-01'AND gender = 'F'
ORDER BY dept_name ASC;
-- 4. Find the current titles of employees currently working in the Customer Service department. Title,employees, departments
SELECT * FROM titles;
SELECT title,COUNT(title) FROM titles
JOIN dept_emp AS D
ON titles.emp_no = D.emp_no
JOIN departments AS dep
ON dep.dept_no = D.dept_no
WHERE titles.to_date = '9999-01-01' AND dept_name = 'Customer Service' AND D.to_date = '9999-01-01'
GROUP BY title ORDER BY title ASC;
-- 5. Find the current salary of all current managers.
#dept_manager
SELECT * FROM dept_manager;
SELECT dept_name AS 'Department Name' ,
CONCAT(first_name,' ',last_name) AS 'Department Manager',
salary
FROM dept_manager AS D
JOIN employees AS E
ON D.emp_no = E.emp_no
JOIN departments AS DEP
ON DEP.dept_no = D.dept_no
JOIN salaries AS S
ON S.emp_no = E.emp_no
WHERE D.to_date = '9999-01-01' AND S.to_date = '9999-01-01' ORDER BY dept_name ASC;
-- 6. Find the number of current employees in each department.
SELECT D.dept_no,dept_name AS 'Department Name', COUNT(dept_name) AS Employee_count
FROM dept_emp AS D
JOIN departments AS Dep
ON D.dept_no = Dep.dept_no
JOIN employees as E
ON D.emp_no = E.emp_no
WHERE D.to_date = '9999-01-01'
GROUP BY D.dept_no ORDER BY D.dept_no;
-- 7. Which department has the highest average salary? Hint: Use current not historic information.department/salaries and deptemp
SELECT dept_name, AVG(salary) FROM salaries AS S
JOIN dept_emp AS emp
ON S.emp_no = emp.emp_no
JOIN departments AS D
ON D.dept_no = emp.dept_no
WHERE S.to_date = '9999-01-01' GROUP BY dept_name ORDER BY AVG(salary) DESC LIMIT 1;
-- 8. Who is the highest paid employee in the Marketing department?
SELECT CONCAT(first_name,' ',last_name) AS Name, salary
FROM employees AS E
JOIN dept_emp AS D
ON E.emp_no = D.emp_no
JOIN departments AS DEP
ON DEP.dept_no = D.dept_no
JOIN salaries AS S
ON S.emp_no = E.emp_no
WHERE dept_name = 'Marketing'
ORDER BY salary DESC LIMIT 1;
-- 9. Which current department manager has the highest salary?
SELECT dept_name AS 'Department Name' ,CONCAT(first_name,' ',last_name) AS 'Department Manager',salary
FROM dept_manager AS D
JOIN employees AS E
ON D.emp_no = E.emp_no JOIN departments AS DEP
ON DEP.dept_no = D.dept_no
JOIN salaries AS S
ON S.emp_no = E.emp_no
WHERE D.to_date = '9999-01-01' AND S.to_date = '9999-01-01' ORDER BY salary DESC LIMIT 1;
-- 10. Determine the average salary for each department. Use all salary information and round your results.
SELECT dept_name, ROUND(AVG(salary),0) FROM salaries AS S
JOIN dept_emp AS emp
ON S.emp_no = emp.emp_no
JOIN departments AS D
ON D.dept_no = emp.dept_no
GROUP BY dept_name ORDER BY AVG(salary) DESC;
-- 11. Bonus Find the names of all current employees, their department name, and their current manager's name.
SELECT CONCAT(first_name,' ',last_name) AS 'Employee Name',
dept_name AS 'Department Name',
DM
FROM employees AS E
JOIN dept_emp AS DEP
ON E.emp_no = DEP.emp_no
JOIN departments AS D
ON D.dept_no = DEP.dept_no
JOIN (SELECT dept_name AS DN ,CONCAT(first_name,' ',last_name) AS DM
FROM dept_manager AS D
JOIN employees AS E
ON D.emp_no = E.emp_no JOIN departments AS DEP
ON DEP.dept_no = D.dept_no WHERE to_date = '9999-01-01' ORDER BY dept_name ASC) AS M
ON DN = dept_name
WHERE DEP.to_date = '9999-01-01';
SELECT * FROM employees AS E
JOIN dept_emp AS DEP
ON E.emp_no = DEP.emp_no
JOIN departments AS D
ON D.dept_no = DEP.dept_no WHERE DEP.to_date = '9999-01-01' ;
#DEP MANAGERS CURRENT
SELECT dept_name AS 'Department Name' ,CONCAT(first_name,' ',last_name) AS 'Department Manager'
FROM dept_manager AS D
JOIN employees AS E
ON D.emp_no = E.emp_no JOIN departments AS DEP
ON DEP.dept_no = D.dept_no WHERE to_date = '9999-01-01' ORDER BY dept_name ASC;
-- ------------------------------------------------------------
-- 2. Using the example in the Associative Table Joins section as a guide, write a query that shows each department along with the name of the current manager for that department.
#dept_manager, departments, employees
SELECT dept_name, CONCAT(first_name,' ',last_name) AS Manager
FROM departments AS D
JOIN dept_manager AS M
ON M.dept_no = D.dept_no
JOIN employees AS E
ON E.emp_no = M.emp_no
WHERE to_date = '9999-01-01' ORDER BY dept_name ASC;
-- 3. Find the name of all departments currently managed by women.
SELECT dept_name, CONCAT(first_name,' ',last_name) AS Manager, gender
FROM departments AS D
JOIN dept_manager AS M
ON M.dept_no = D.dept_no
JOIN employees AS E
ON E.emp_no = M.emp_no
WHERE to_date = '9999-01-01' AND gender = 'F' ORDER BY dept_name ASC;
-- 4. Find the current titles (title count)of employees currently working in the Customer Service department. Title,departments
SELECT title,COUNT(title) FROM titles AS T
JOIN dept_emp AS DE
ON T.emp_no = DE.emp_no
JOIN departments AS D
ON D.dept_no = DE.dept_no
WHERE T.to_date = '9999-01-01' AND dept_name = 'Customer Service'
GROUP BY title ORDER BY title ASC;
-- 5. Find the current salary of all current managers.
SELECT dept_name,
CONCAT(first_name,' ',last_name) AS Manager,
salary
FROM departments AS D
JOIN dept_manager AS M
ON M.dept_no = D.dept_no
JOIN employees AS E
ON E.emp_no = M.emp_no
JOIN salaries AS S
ON S.emp_no = M.emp_no
WHERE M.to_date = '9999-01-01' AND S.to_date = '9999-01-01'
ORDER BY dept_name ASC;
-- 6. Find the number of current employees in each department.
#departments
SELECT D.dept_no,dept_name,COUNT(*) FROM departments AS D
JOIN dept_emp AS DE
ON DE.dept_no = D.dept_no
WHERE to_date = '9999-01-01'
GROUP BY dept_no
ORDER BY dept_no ASC;
-- 7. Which department has the highest average salary? Hint: Use current not historic information.
SELECT dept_name,AVG(salary) from departments AS D
JOIN dept_emp AS DE
ON DE.dept_no = D.dept_no
JOIN salaries AS S
ON S.emp_no = DE.emp_no
WHERE S.to_date = '9999-01-01'
GROUP BY dept_name ORDER BY AVG(salary) DESC LIMIT 1;
-- 8. Who is the highest paid employee in the Marketing department?
SELECT * FROM employees AS E
JOIN dept_emp AS DE
ON DE.emp_no = E.emp_no
JOIN departments AS D
ON D.dept_no = DE.dept_no
JOIN salaries AS S
ON S.emp_no = E.emp_no
WHERE dept_name = 'Marketing'
ORDER BY salary DESC LIMIT 1;
-- 9. Which current department manager has the highest salary?
SELECT dept_name, CONCAT(first_name,' ',last_name) AS Manager, salary
FROM departments AS D
JOIN dept_manager AS M
ON M.dept_no = D.dept_no
JOIN employees AS E
ON E.emp_no = M.emp_no
JOIN salaries AS S
ON S.emp_no = E.emp_no
WHERE M.to_date = '9999-01-01' AND S.to_date = '9999-01-01' ORDER BY dept_name ASC;
--
-- 10. Determine the average salary for each department. Use all salary information and round your results.
SELECT dept_name,ROUND(AVG(salary),0) from departments AS D
JOIN dept_emp AS DE
ON DE.dept_no = D.dept_no
JOIN salaries AS S
ON S.emp_no = DE.emp_no
GROUP BY dept_name ORDER BY AVG(salary) DESC;