-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path15.Employees_SQL_SUBQUERIES.sql
More file actions
79 lines (69 loc) · 2.07 KB
/
15.Employees_SQL_SUBQUERIES.sql
File metadata and controls
79 lines (69 loc) · 2.07 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
-- Extract the information about all department managers who were hired between the 1st of January 1990 and the 1st of January 1995.
SELECT *
FROM dept_manager
WHERE emp_no IN (
SELECT emp_no
FROM employees
WHERE hire_date BETWEEN '1990-01-01' AND '1995-01-01'
);
-- Select the entire information for all employees whose job title is “Assistant Engineer”.
-- Hint: To solve this exercise, use the 'employees' table.
SELECT *
FROM employees e
WHERE EXISTS (
SELECT 1
FROM titles t
WHERE t.title = 'Assistant Engineer'
AND e.emp_no = t.emp_no
);
-- Starting your code with “DROP TABLE”, create a table called “emp_manager”
-- (emp_no – integer of 11, not null; dept_no – CHAR of 4, null; manager_no – integer of 11, not null).
DROP TABLE IF EXISTS emp_manager;
CREATE TABLE emp_manager (
emp_no INT NOT NULL,
dept_no CHAR(4) NULL,
manager_no INT NOT NULL
);
-- Fill emp_manager with data about employees, the number of the department they are working in, and their managers.
-- Your output must contain 42 rows.
INSERT INTO emp_manager (emp_no, dept_no, manager_no)
SELECT
u.emp_no, u.dept_no, u.manager_no
FROM (
SELECT
e.emp_no AS emp_no,
MIN(de.dept_no) AS dept_no,
110022 AS manager_no
FROM employees e
JOIN dept_emp de ON e.emp_no = de.emp_no
WHERE e.emp_no <= 10020
GROUP BY e.emp_no
UNION
SELECT
e.emp_no AS emp_no,
MIN(de.dept_no) AS dept_no,
110039 AS manager_no
FROM employees e
JOIN dept_emp de ON e.emp_no = de.emp_no
WHERE e.emp_no > 10020
GROUP BY e.emp_no
LIMIT 20
UNION
SELECT
e.emp_no AS emp_no,
MIN(de.dept_no) AS dept_no,
110039 AS manager_no
FROM employees e
JOIN dept_emp de ON e.emp_no = de.emp_no
WHERE e.emp_no = 110022
GROUP BY e.emp_no
UNION
SELECT
e.emp_no AS emp_no,
MIN(de.dept_no) AS dept_no,
110022 AS manager_no
FROM employees e
JOIN dept_emp de ON e.emp_no = de.emp_no
WHERE e.emp_no = 110039
GROUP BY e.emp_no
) u;