SQL · Module 04: Mastering JOINs & Set OperationsTutorial 12 of 24

4.3 Set Operations: UNION vs UNION ALL, INTERSECT & EXCEPT

Stack query results vertically with UNION and UNION ALL, understand why UNION ALL is dramatically faster, and find overlaps or differences between datasets with INTERSECT and EXCEPT.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. JOIN vs UNION — Horizontal vs Vertical Combination

Beginners often mix these up. Remember this 1-second visual rule:

  • JOIN combines tables horizontally (side-by-side) — adds more columns based on a matching key.
  • UNION / INTERSECT / EXCEPT combines query results vertically (top-to-bottom) — adds or filters rows from queries with the exact same number of columns and compatible types.

2. UNION vs UNION ALL (Top Performance Interview Question!)

FeatureUNIONUNION ALL
Duplicate Rows❌ Removes duplicates (like DISTINCT)✅ Keeps ALL rows (including duplicates)
PerformanceSlower — requires an expensive Sort / Hash Deduplication step across all rowsBlazing fast (O(N)) — simply streams Result A followed by Result B!
When to UseOnly when you genuinely need duplicates eliminatedDefault choice whenever rows are already disjoint (e.g., orders_2025 + orders_2026)
SQL
-- Fast: Combine 2025 archive orders and 2026 active orders (already disjoint!)
SELECT order_id, user_id, amount, '2025' AS fiscal_year FROM orders_2025
UNION ALL
SELECT order_id, user_id, amount, '2026' AS fiscal_year FROM orders_2026
ORDER BY amount DESC;

Formatting Rule: Column names in the final output always come from the FIRST SELECT, and ORDER BY / LIMIT goes at the very end (after the last query)!


3. INTERSECT & EXCEPT (Finding Overlaps & Differences)

OperatorMath EquivalentWhat It ReturnsDialect Note
UNIONA ∪ B (unique)Rows in A or B (deduplicated)All SQL DBs
UNION ALLA + BAll rows from A followed by all rows from BAll SQL DBs
INTERSECTA ∩ BRows present in BOTH A and BPostgres, SQLite, SQL Server, MySQL 8.0.31+
EXCEPTA − BRows in A that are NOT in BCalled MINUS in Oracle!
SQL
-- Users who completed BOTH the Python track AND the SQL track
SELECT user_id FROM python_completions
INTERSECT
SELECT user_id FROM sql_completions;

-- Users who completed Python but have NOT started SQL yet!
SELECT user_id FROM python_completions
EXCEPT
SELECT user_id FROM sql_completions;

Watch UNION, INTERSECT & EXCEPT Side-by-Step

Tracing EXCEPT (A − B): Active Users Who Never Bought
Interactive SQL Execution Tracer · Step 1 of 3 (1. Query A (active_users))
1→SELECT user_id FROM active_users
2EXCEPT
3SELECT user_id FROM paid_orders;
Live Table State · 1. Query A (active_users)4 rows
user_id
101
102
103
104
What the DB engine just did: Query A returns 4 active user IDs: 101, 102, 103, 104.

4. Quick Check Your Understanding

Module 4.3 Quick Quiz

Q1.Why is UNION ALL almost always faster than UNION?
Q2.What requirement MUST be true to combine two SELECT statements with UNION?

Keep Exploring: Related Lessons & Tools

All Courses →