Set - EXCEPT & INTERSECT
set difference or intersection; NULLs compare equal here
What changes
- Input
- two query results with matching column count and compatible types
- Output or change
- distinct rows in left not in right (EXCEPT) or in both (INTERSECT)
Syntax at a glance
{ <query_specification> | ( <query_expression> ) } { EXCEPT | INTERSECT } { <query_specification> | ( <query_expression> ) }
Work example
A report needs Set - EXCEPT & INTERSECT to define which rows satisfy a compound search condition.
Test a match, a nonmatch, and NULL. Write the expected TRUE, FALSE, or UNKNOWN result before running the filter.
Check your work Set - EXCEPT & INTERSECT. The rows kept by Set - EXCEPT & INTERSECT are exactly those whose complete condition evaluates to TRUE.
Practice SQL
Open in SQL FlowSELECT Code FROM (VALUES (1), (2)) AS FirstSet(Code)
INTERSECT
SELECT Code FROM (VALUES (2), (3)) AS SecondSet(Code);
Open Microsoft Learn for full syntax, permissions, and details for your version.