diff --git a/SQL - MySQL for Data Analytics and Business Intelligence/15.SQL Subqueries/15.Employees_SQL_SUBQUERIES.sql b/SQL - MySQL for Data Analytics and Business Intelligence/15.SQL Subqueries/15.Employees_SQL_SUBQUERIES.sql index a7a8421..4dc050c 100644 --- a/SQL - MySQL for Data Analytics and Business Intelligence/15.SQL Subqueries/15.Employees_SQL_SUBQUERIES.sql +++ b/SQL - MySQL for Data Analytics and Business Intelligence/15.SQL Subqueries/15.Employees_SQL_SUBQUERIES.sql @@ -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;