The SQL INTERSECT query is similar to the SQL UNION query, also works on two SQL Select statements. The difference is that SQL UNION query selects rows either in the first or the second Select statement (like OR operator), while the SQL INTERSECT query selects rows in the both first and second Select statement (like AND operator).
Note: SQL INTERSECT query only returns distinct values.
SQL INTERSECT Query Syntax
SELECT Column(s) FROM Table1 INTERSECT SELECT Column(s) FROM Table2
SQL INTERSECT Query Example
Table: FinanceEmployees
EmployeeId | FirstName | LastName |
---|---|---|
203 | Kilooa | Joooo |
204 | Hioiaaa | Hifa |
205 | Yuiofa | Iioooa |
206 | Miofa | Poqqq |
EmployeeId | FirstName | LastName |
---|---|---|
206 | Miofa | Poqqq |
207 | Qifaww | Siafoa |
208 | Ziooo | Rrroo |
209 | UUifad | Bbiiw |
Select all the employees in both FinanceEmployees and DevelopmentEmployees:
SELECT EmployeeId, FirstName, LastName FROM FinanceEmployees INTERSECT SELECT EmployeeId, FirstName, LastName FROM DevelopmentEmployees
The result will look like:
EmployeeId | FirstName | LastName |
---|---|---|
206 | Miofa | Poqqq |