SQL/Joins 6 min

UNION, INTERSECT and EXCEPT

Stack and compare whole result sets.

unionintersectexcept

UNION ALL stacks two results on top of each other; both sides need the same number of columns in the same order. UNION does the same but removes duplicates, which costs a sort.

sql · editable
loading editor…

Try it: Restrict the product side to the Audio category.

INTERSECT keeps rows present in both results and EXCEPT keeps rows present in the first but not the second — a quick way to diff two populations.

sql · editable
loading editor…

Prefer UNION ALL unless you actually need de-duplication — it is cheaper, and it keeps legitimately repeated rows.

practice this