SQL · Module 04: Mastering JOINs & Set Operations·Tutorial 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:
JOINcombines tables horizontally (side-by-side) — adds more columns based on a matching key.UNION/INTERSECT/EXCEPTcombines 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!)
| Feature | UNION | UNION ALL |
|---|---|---|
| Duplicate Rows | ❌ Removes duplicates (like DISTINCT) | ✅ Keeps ALL rows (including duplicates) |
| Performance | Slower — requires an expensive Sort / Hash Deduplication step across all rows | Blazing fast (O(N)) — simply streams Result A followed by Result B! |
| When to Use | Only when you genuinely need duplicates eliminated | Default 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, andORDER BY/LIMITgoes at the very end (after the last query)!
3. INTERSECT & EXCEPT (Finding Overlaps & Differences)
| Operator | Math Equivalent | What It Returns | Dialect Note |
|---|---|---|---|
UNION | A ∪ B (unique) | Rows in A or B (deduplicated) | All SQL DBs |
UNION ALL | A + B | All rows from A followed by all rows from B | All SQL DBs |
INTERSECT | A ∩ B | Rows present in BOTH A and B | Postgres, SQLite, SQL Server, MySQL 8.0.31+ |
EXCEPT | A − B | Rows in A that are NOT in B | Called 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 →DSA Lesson
Recursion Fundamentals & Subsets
SQL Lesson
Subqueries: Scalar, Multi-Row (IN / ANY / ALL) & Correlated
DSA Lesson
Dijkstra's Shortest Path & Disjoint Set Union (Union-Find)
Free Browser Tool
Big-O & Code Analyzer
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.
Free Browser Tool
Regex Tester & Explainer
Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.