SQL Interview & Case Lab
Back to Interview SQL Patterns

EXCEPT, INTERSECT, UNION — For Diffs and Dedup

When self-joins are overkill — the set operations every analyst should reach for. FIND_VIDEO: search 'SQL EXCEPT INTERSECT UNION ALL' — recommended channel: Modern SQL. Aim for 9 min or under.

11 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Set Operation Overview — Introduction to sample tables and structural requirements for SQL set operations.
  2. UNION Demonstration — Executing UNION to combine two datasets while removing overlapping duplicate records.
  3. UNION ALL Execution — Running UNION ALL to demonstrate full dataset concatenation with duplicate rows preserved.
  4. INTERSECT Operation — Using INTERSECT to isolate rows that exist concurrently in both tables.
  5. EXCEPT (A - B) — Demonstrating set subtraction from u1 to u2 to extract unique left-table rows.
  6. EXCEPT (B - A) — Inverting the EXCEPT query order to extract rows unique to the right table.
PDF notes

Frequently asked questions

Why does EXCEPT return different rows when changing table order?

EXCEPT computes set difference (A - B). Inverting the order computes (B - A), isolating records unique to table B instead of table A.

Does INTERSECT preserve duplicate matching rows?

No, INTERSECT returns only distinct rows that exist across both datasets, automatically stripping duplicate occurrences.

What happens if column names differ between the two SELECT queries?

The query executes normally as long as the positional datatypes match; the final output column headers inherit names from the first SELECT query.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.