Set Operations

Lesson 13 of 20

UNION stacks, INTERSECT overlaps, EXCEPT subtracts — combine whole result grids like building blocks.

The big picture

Combining whole results

WHERE filters rows inside one query. Set operators do something bigger: they take two entire query results and stack, overlap or subtract them.

UNION

Glues two result grids on top of each other and removes duplicate rows.

UNION ALL

Same stacking, but duplicates stay. It is faster because nothing is checked.

INTERSECT and EXCEPT

INTERSECT keeps rows present in both results; EXCEPT keeps rows from the first that miss the second.

The one rule

Both queries must return the same number of columns, with compatible types.