Set Operations
Set Operations
Section titled “Set Operations”Set operations combine results from multiple SELECT queries into a single result. They work on rows, not columns.
Real-World Analogy
Section titled “Real-World Analogy”Think of two guest lists:
- UNION: Everyone who’s on EITHER list (no duplicates)
- UNION ALL: Everyone on both lists (including duplicates)
- INTERSECT: People on BOTH lists
- EXCEPT: People on list A but NOT on list B
Visual Guide
Section titled “Visual Guide”flowchart TB subgraph Sets[Set Operations — Venn Diagrams] UNION["UNION<br/>A ∪ B<br/>All from both, no dupes"] UNIONALL["UNION ALL<br/>All from both, keep dupes"] INTERSECT["INTERSECT<br/>A ∩ B<br/>Only in both"] EXCEPT["EXCEPT<br/>A − B<br/>In A but not B"] end
style UNION fill:#7c3aed,color:#fff style UNIONALL fill:#3b82f6,color:#fff style INTERSECT fill:#059669,color:#fff style EXCEPT fill:#f59e0b,color:#fffSample Data
Section titled “Sample Data”-- Employees who know SQLSELECT name FROM sql_team;-- Alice, Bob, Carol
-- Employees who know PythonSELECT name FROM python_team;-- Carol, David, EveUNION vs UNION ALL
Section titled “UNION vs UNION ALL”-- UNION: removes duplicates (slower — sorts to detect dupes)SELECT name FROM sql_teamUNIONSELECT name FROM python_team;-- Result: Alice, Bob, Carol, David, Eve (Carol appears once)
-- UNION ALL: keeps duplicates (faster)SELECT name FROM sql_teamUNION ALLSELECT name FROM python_team;-- Result: Alice, Bob, Carol, Carol, David, Eve (Carol appears twice)| Feature | UNION | UNION ALL |
|---|---|---|
| Duplicates | Removed | Kept |
| Performance | Slower (sorts to deduplicate) | Fast |
| Use when | You want distinct results | You know there are no duplicates |
INTERSECT
Section titled “INTERSECT”-- Employees who know BOTH SQL AND PythonSELECT name FROM sql_teamINTERSECTSELECT name FROM python_team;-- Result: Carol (only Carol is in both teams)EXCEPT
Section titled “EXCEPT”-- Employees who know SQL but NOT PythonSELECT name FROM sql_teamEXCEPTSELECT name FROM python_team;-- Result: Alice, Bob (they know SQL but not Python)Rules for Set Operations
Section titled “Rules for Set Operations”1. Same number of columns in all SELECT statements2. Corresponding columns must have compatible data types3. Column names come from the FIRST SELECT4. ORDER BY goes at the VERY END (applies to whole result)-- ✅ Correct: same columns, same typesSELECT id, name FROM employeesUNIONSELECT id, name FROM contractors;
-- ❌ Wrong: different number of columnsSELECT id, name FROM employeesUNIONSELECT id FROM contractors;
-- ❌ Wrong: incompatible typesSELECT id, salary FROM employeesUNIONSELECT id, name FROM contractors; -- salary vs name - types don't matchORDER BY with Set Operations
Section titled “ORDER BY with Set Operations”-- ORDER BY goes at the very endSELECT name, salary FROM employees WHERE dept_id = 1UNIONSELECT name, salary FROM employees WHERE dept_id = 2ORDER BY salary DESC;
-- Use column position or alias from the first SELECTSELECT name AS employee_name, salaryFROM employees WHERE dept_id = 1UNIONSELECT name, salary FROM employees WHERE dept_id = 2ORDER BY employee_name;Practical Examples
Section titled “Practical Examples”Combine active and archived orders:
SELECT id, customer_id, total, 'active' AS sourceFROM ordersUNION ALLSELECT id, customer_id, total, 'archived'FROM orders_archiveORDER BY id;Find customers who never ordered:
-- Customers with no ordersSELECT id, name FROM customersEXCEPTSELECT customer_id, customer_name FROM orders;Find products in both categories (intersection):
SELECT product_id FROM inventoryINTERSECTSELECT product_id FROM current_promotions;In Simple Words
Section titled “In Simple Words”- UNION combines queries and removes duplicates; UNION ALL keeps them
- INTERSECT returns rows that exist in BOTH queries
- EXCEPT returns rows from the first query that are NOT in the second
- All SELECTs must have the same number of columns with compatible types
- ORDER BY goes at the very end — it sorts the final combined result