Last answered:

13 Jun 2022

Posted on:

31 May 2022

1

15. SQL Subqueries nested in SELECT and FROM - Using Employees and Dept_emp table only

Hello Team,

Just to clear all doubt. The task presented in this lecture clearly highlight the use of two tables which are "Employees table" and "Dept_manager table" only. But during the lecture you captured the "Dept_emp table". I didn't get any clear reason why it was suddenly introduced into the query.
So i decided to re-write my query as shown below neglecting the "dept_manager table" totally. And to see if i will come up with the same output giving in the lecture. To my surprise it did.

My question. Is my query still adoptable? And how come it responded so well when i used just "employees table" and "Dept_emp tables" only. Disregarding the "dept_manager table" through out my query.

Great Job 365 Team!!!



SELECT
    A.*
FROM
    (SELECT
        e.emp_no AS employee_Id,
            MIN(de.dept_no) AS department_Code,
            (SELECT
                    de.emp_no
                FROM
                    dept_emp de
                WHERE
                    emp_no = 110022) AS manager_Id
    FROM
        employees e
    JOIN dept_emp de ON e.emp_no = de.emp_no
    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
                    de.emp_no
                FROM
                    dept_emp de
                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 > 10020
    GROUP BY e.emp_no
    ORDER BY e.emp_no
    LIMIT 20) AS B;

1 answers ( 0 marked as helpful)
Instructor
Posted on:

13 Jun 2022

0

Hi Ashakah!
Thanks for reaching out.

Yes, your code is correct. Bravo!

Hope this helps.
Best,
Tsvetelin

Submit an answer