INTERSECT
returns records found in both recordsets
UNION
returns records found in any of the recordsets.
EXCEPT
returns records found in the first recordset but not in the second one.
Since both INTERSECT
and EXCEPT
can only return the records found in the first recordset, they can be rewritten using joins or IN
/ NOT IN
predicates.
However, they are sometimes more convenient, since SQL Server
does not support IN
expressions on multiple columns.
INTERSECT
and EXCEPT
, unlike JOIN
and IN
, treat NULL
values as matching. This query:
SELECT NULL
INTERSECT
SELECT NULL
returns a record, while this one:
SELECT NULL
WHERE NULL IN
(
SELECT NULL
)
returns nothing.