TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2793 929
If 2025: 101, 102, 103 and 2026: 102, 103, 104 → Result: 102, 103

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;
  • ❤ 2
More from @sqlanalyst
  1. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  2. Oct 7, 2026SQL Interview Series — Part 4 📌 Question 4: Find the Highest Salary in Each Department Su…
  3. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
  4. Sep 29, 2026SQL Interview Series — Part 2 📌 Question 2: Find Duplicate Records Suppose you have an Em…
  5. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  6. Sep 29, 2026SQL Interview Series — Part 1 Hi guys, let's start a SQL interview series covering frequen…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →