SQL INTERSECT

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

Table: DevelopmentEmployees

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>