Real-world use - Find customers who purchased in both years:
SELECT customer_id FROM purchases_2025
INTERSECT
SELECT customer_id FROM purchases_2026;
9️⃣ EXCEPT
"EXCEPT" returns rows from the first query that don't exist in the second query.
SELECT customer_id FROM customers_2025
EXCEPT
SELECT customer_id FROM customers_2026;
Result: 101. In Oracle, the equivalent is commonly: MINUS
🔟 Understanding the Four Operators
Suppose: A = {1, 2, 3}, B = {2, 3, 4}
• UNION → {1, 2, 3, 4}
• UNION ALL → {1, 2, 3, 2, 3, 4}
• INTERSECT → {2, 3}
• EXCEPT → {1}
This is the easiest way to remember them.
1️⃣1️⃣ UNION for Combining Similar Data
SELECT order_id, customer_id, sales_amount FROM online_sales
UNION ALL
SELECT order_id, customer_id, sales_amount FROM store_sales;
1️⃣2️⃣ Current + Historical Data
SELECT * FROM current_transactions
UNION ALL
SELECT * FROM historical_transactions;
1️⃣3️⃣ Finding Common Customers
SELECT customer_id FROM product_a_users
INTERSECT
SELECT customer_id FROM product_b_users;
1️⃣4️⃣ Finding Customers Who Stopped Using a Product
SELECT customer_id FROM product_a_users
EXCEPT
SELECT customer_id FROM product_b_users;
1️⃣5️⃣ Set Operators vs JOINs
JOIN - JOIN combines columns from related tables.
SELECT c.customer_id, c.customer_name, o.order_amount
FROM customers c JOIN orders o ON c.customer_id = o.customer_id;
UNION - UNION combines rows from compatible queries.
Simple rule: JOIN → Add columns, UNION → Add rows
1️⃣6️⃣ UNION vs JOIN — Example
Table A: 101 Rahul, 102 Priya | Table B: 103 Amit, 104 Neha
To stack the records:
SELECT customer_id, name FROM table_a
UNION ALL
SELECT customer_id, name FROM table_b;
Result: 101 Rahul, 102 Priya, 103 Amit, 104 Neha
1️⃣7️⃣ Using Set Operators with Filters
SELECT customer_id FROM customers_2025 WHERE country = 'India'
UNION
SELECT customer_id FROM customers_2026 WHERE country = 'India';
1️⃣8️⃣ Set Operators with Aggregation
SELECT region, SUM(sales_amount) AS total_sales FROM sales_2025 GROUP BY region
UNION ALL
SELECT region, SUM(sales_amount) AS total_sales FROM sales_2026 GROUP BY region;
For one combined total per region:
SELECT region, SUM(sales_amount) AS total_sales FROM (
SELECT region, sales_amount FROM sales_2025
UNION ALL
SELECT region, sales_amount FROM sales_2026
) s GROUP BY region;
1️⃣9️⃣ NULL Values with Set Operators
NULL values can participate in set operations. If both result sets contain NULL, duplicate elimination treats the corresponding rows as duplicates for set-operation purposes.
2️⃣0️⃣ Common Mistakes
❌ Mistake 1: Different number of columns
❌ Mistake 2: Incompatible data types
❌ Mistake 3: Using UNION when duplicates are required - Use UNION ALL when appropriate.
❌ Mistake 4: Confusing JOIN and UNION - Remember: JOIN → combine related columns, UNION → combine compatible rows
🎯 Business Example
SELECT COUNT(*) AS payment_count FROM (
SELECT payment_id FROM payments_2025
UNION ALL
SELECT payment_id FROM payments_2026
) p;
SELECT SUM(payment_amount) AS total_payment_value FROM (
SELECT payment_amount FROM payments_2025
UNION ALL
SELECT payment_amount FROM payments_2026
) p;