Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SQL queries and table creation for department managers and employees #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,108 +1,79 @@
/*Extract the information about all department managers who were hired between the 1st of January 1990 and the 1st of January 1995.*/
-- 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
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 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 emp_no FROM titles t
WHERE title LIKE 'Assistant Engineer'
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). */
-- 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,
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
-- 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.*
FROM
(SELECT
a.*
FROM
(SELECT
e.emp_no AS employee_ID,
MIN(de.dept_no) AS department_code,
(SELECT
emp_no
FROM
dept_manager
WHERE
emp_no = 110022) AS manager_ID
FROM
employees e
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
WHERE e.emp_no <= 10020
GROUP BY e.emp_no
ORDER BY e.emp_no) AS a UNION SELECT
b.*
FROM
(SELECT
e.emp_no AS employee_ID,
MIN(de.dept_no) AS department_code,
(SELECT
emp_no
FROM
dept_manager
WHERE
emp_no = 110039) AS manager_ID
FROM
employees e

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
WHERE e.emp_no > 10020
GROUP BY e.emp_no
ORDER BY e.emp_no
LIMIT 20) AS b UNION SELECT
c.*
FROM
(SELECT
e.emp_no AS employee_ID,
MIN(de.dept_no) AS department_code,
(SELECT
emp_no
FROM
dept_manager
WHERE
emp_no = 110039) AS manager_ID
FROM
employees e
JOIN dept_emp de ON e.emp_no = de.emp_no
WHERE
e.emp_no = 110022
GROUP BY e.emp_no) AS c UNION SELECT
d.*
FROM
(SELECT
e.emp_no AS employee_ID,
MIN(de.dept_no) AS department_code,
(SELECT
emp_no
FROM
dept_manager
WHERE
emp_no = 110022) AS manager_ID
FROM
employees e
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 = 110039
GROUP BY e.emp_no) AS d) as u;
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;