简体   繁体   中英

Join 3 tables with where clause without common table

I have 3 tables employee, sales, department

Employee table

------------------------------
  AssignemtID | EmployeeEmail
-------------------------------
      1       | abc.@cam.com
-------------------------------
      2       | bdc@cam.com
-----------------------------
      3       | dgs@cam.com
-----------------------------
      4       | tyr@cam.com
------------------------------

Sales table

Sales | EmployeeEmail     | Emp ID
------------------------------------
24333  | abc.@cam.com  |  34
------------------------------------
46373 | bdc@cam.com    | 24
-----------------------------------
32212 | dgs@cam.com    | 78
-----------------------------------

Department table

Department | Sales       |Emp ID
-------------------------------
AS         | 24333       |34
-------------------------------
we         | 46373       |24
-----------------------------
de         | 32212       |78
------------------------------

I want to see all the employee who are there in the Employee table but not in the Department table. the data is huge in all the 3 tables

You can connect Employee to Department by joining it with Sales .

SELECT *
FROM Employee
WHERE EmployeeEmail NOT IN (
    SELECT EmployeeEmail
    FROM Sales AS s
    JOIN Department AS d ON s.Sales = d.Sales
)

or

SELECT e.*
FROM Employee AS e
LEFT JOIN Sales AS s ON s.EmployeeEmail = e.EmployeeEmail
LEFT JOIN Department AS d ON s.Sales = d.Sales
WHERE d.Department IS NULL
SELECT *
FROM Employee
WHERE EmployeeEmail NOT IN (
    SELECT EmployeeEmail
    FROM Sales AS s
    JOIN Department AS dep ON s.Sales = dep.Sales
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM