-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase_exercises.sql
More file actions
142 lines (112 loc) · 4.85 KB
/
case_exercises.sql
File metadata and controls
142 lines (112 loc) · 4.85 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
-- 1. Write a query that returns all employees, their department number, their start date, their end date, and a new column 'is_current_employee' that is a 1 if the employee is still with the company and 0 if not. DO NOT WORRY ABOUT DUPLICATE EMPLOYEES.
-- dept_no, emp name and number, start date(hire date), end date (to_date), and is_current_employee
USE employees;
SELECT * FROM employees;
SELECT dept_no, E.emp_no, first_name, last_name, from_date,to_date,
IF(to_date > CURDATE(),True,False) AS is_currently_employee
FROM employees AS E
JOIN dept_emp AS DE
ON DE.emp_no = E.emp_no;
-- 2. Write a query that returns all employee names (previous and current), and a new column 'alpha_group' that returns 'A-H', 'I-Q', or 'R-Z' depending on the first letter of their last name.
SELECT first_name, last_name,
CASE
WHEN SUBSTR(last_name,1,1) BETWEEN 'A' AND 'H' THEN 'A-H'
WHEN SUBSTR(last_name,1,1) BETWEEN 'I' AND 'Q' THEN 'I-Q'
ELSE 'R-Z'
END AS alpha_group
FROM employees;
-- 3. How many employees (current or previous) were born in each decade?
SELECT * FROM employees ORDER BY birth_date DESC; #1952 oldest 1965 youngest
SELECT COUNT(*) FROM employees; #300024 total
SELECT COUNT(*),
CASE
WHEN birth_date LIKE '195%' THEN '50s'
WHEN birth_date LIKE '196%' THEN '60s'
END AS decade
FROM employees
GROUP BY decade;
#182886 born in 50s
#117138 born in 60s
-- 4. What is the current average salary for each of the following department groups: R&D, Sales & Marketing, Prod & QM, Finance & HR, Customer Service?
# depeartment, AVG salary
SELECT * FROM departments AS D
JOIN dept_emp AS DE
ON DE.dept_no = D.dept_no
JOIN salaries AS S
ON DE.emp_no = S.emp_no
WHERE DE.to_date > CURDATE() AND S.to_date > CURDATE(); #every branch and the average current salaries from current employees of that branch
SELECT *,
CASE
WHEN dept_name IN ('research', 'development') THEN 'R&D'
WHEN dept_name IN ('sales', 'marketing') THEN 'Sales & Marketing'
WHEN dept_name IN ('Production', 'Quality Management') THEN 'Prod & QM'
WHEN dept_name In ('Finance','Human Resources') THEN 'Finance & HR'
ELSE dept_name
END AS dept_group
FROM departments;
SELECT dept_group, AVG(salary) FROM departments AS D
JOIN dept_emp AS DE
ON DE.dept_no = D.dept_no
JOIN salaries AS S
ON DE.emp_no = S.emp_no
JOIN (SELECT *,
CASE
WHEN dept_name IN ('research', 'development') THEN 'R&D'
WHEN dept_name IN ('sales', 'marketing') THEN 'Sales & Marketing'
WHEN dept_name IN ('Production', 'Quality Management') THEN 'Prod & QM'
WHEN dept_name In ('Finance','Human Resources') THEN 'Finance & HR'
ELSE dept_name
END AS dept_group
FROM departments) AS G
ON G.dept_no = DE.dept_no
WHERE DE.to_date > CURDATE() AND S.to_date > CURDATE()
GROUP BY dept_group;
-- SELECT dept_group,AVG(average) FROM (SELECT *,
-- CASE
-- WHEN dept_name IN ('research', 'development') THEN 'R&D'
-- WHEN dept_name IN ('sales', 'marketing') THEN 'Sales & Marketing'
-- WHEN dept_name IN ('Production', 'Quality Management') THEN 'Prod & QM'
-- WHEN dept_name In ('Finance','Human Resources') THEN 'Finance & HR'
-- ELSE dept_name
-- END AS dept_group
-- FROM
-- (SELECT dept_name, AVG(salary) AS average FROM departments AS D
-- JOIN dept_emp AS DE
-- ON DE.dept_no = D.dept_no
-- JOIN salaries AS S
-- ON DE.emp_no = S.emp_no
-- WHERE DE.to_date > CURDATE() AND S.to_date > CURDATE()
-- GROUP BY dept_name) AS A) AS B
-- GROUP BY dept_group;
-- BONUS
-- Remove duplicate employees from exercise 1.
SELECT dept_no, E.emp_no, first_name, last_name, from_date,to_date,
IF(to_date > CURDATE(),True,False) AS is_currently_employee
FROM employees AS E
JOIN dept_emp AS DE
ON DE.emp_no = E.emp_no; #code for exercise 1
SELECT * FROM dept_emp WHERE emp_no = 11092 OR emp_no = 15499; #test for emp_no to see what could cause double entries
SELECT E.emp_no, COUNT(E.emp_no)
FROM employees AS E
GROUP BY emp_no
ORDER BY COUNT(E.emp_no) DESC
; # check if employees table is correct. only one entry for each employee number. YES
SELECT E.emp_no, COUNT(E.emp_no)
FROM employees AS E
JOIN dept_emp AS DE
ON DE.emp_no = E.emp_no
GROUP BY emp_no
ORDER BY COUNT(E.emp_no) DESC; # count for joined tables. THIS TABLE HAS DUPLICATES.
SELECT E.emp_no, COUNT(E.emp_no)
FROM employees AS E
JOIN dept_emp AS DE
ON DE.emp_no = E.emp_no
WHERE to_date > CURDATE()
GROUP BY emp_no
ORDER BY COUNT(E.emp_no) DESC; #count for joined tables. WHERE it only shows the employee once for the current branch they work in.
SELECT dept_no, E.emp_no, first_name, last_name, from_date,to_date,
IF(to_date > CURDATE(),True,False) AS is_currently_employee
FROM employees AS E
JOIN dept_emp AS DE
ON DE.emp_no = E.emp_no
WHERE to_date > CURDATE(); #will filter out all old employees and leaves only current employees therefore removing duplicates.