TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2792 924
๐Ÿš€ SQL Roadmap 2026 โ€” Part 16

SQL Set Operators โ€” UNION, UNION ALL, INTERSECT & EXCEPT

Set operators allow you to combine the results of multiple SELECT queries.

They are extremely useful when you have similar datasets and want to:

โ€ข Combine records from different sources

โ€ข Remove duplicates

โ€ข Find common records

โ€ข Find records existing in one dataset but not another

โ€ข Compare two datasets

โ€ข Combine current and historical data

The four important set operators are: UNION, UNION ALL, INTERSECT, EXCEPT



In Oracle, "MINUS" is commonly used instead of "EXCEPT".



1๏ธโƒฃ What Are Set Operators?

Suppose you have two tables: customers_2025 and customers_2026. Both contain: customer_id, customer_name. You want one result containing customers from both years.

You can use:

SELECT customer_id, customer_name FROM customers_2025
UNION
SELECT customer_id, customer_name FROM customers_2026;


The result combines the two result sets.

2๏ธโƒฃ UNION

"UNION" combines two result sets and removes duplicate rows.

SELECT customer_id FROM customers_2025
UNION
SELECT customer_id FROM customers_2026;


If customer "101" appears in both tables, it appears only once in the final result.

Example: 2025: 101, 102, 103 | 2026: 102, 103, 104 | Result: 101, 102, 103, 104

When to use UNION?

Use "UNION" when duplicate rows should be removed.

SELECT email FROM online_customers
UNION
SELECT email FROM store_customers;


This can produce a unique list of customer emails across both channels.

3๏ธโƒฃ UNION ALL

"UNION ALL" also combines result sets, but keeps duplicates.

SELECT customer_id FROM customers_2025
UNION ALL
SELECT customer_id FROM customers_2026;


Using the previous example: Result: 101, 102, 103, 102, 103, 104. The duplicate records remain.

4๏ธโƒฃ UNION vs UNION ALL

This is one of the most common SQL interview questions.

UNION โ†’ Combines results โ†’ Removes duplicates

UNION ALL โ†’ Combines results โ†’ Keeps duplicates

Performance consideration: "UNION" generally needs additional work to identify and remove duplicates.

"UNION ALL" does not need that deduplication step. Therefore, when duplicates are valid and you don't need to remove them, "UNION ALL" is generally preferable.

5๏ธโƒฃ Rules for Using Set Operators

The queries being combined must be compatible.

SELECT customer_id, customer_name FROM customers
UNION
SELECT customer_id, customer_name FROM archived_customers;


Both queries return 2 columns - This is valid.

But:

SELECT customer_id, customer_name FROM customers
UNION
SELECT customer_id FROM archived_customers;


is invalid because the number of columns doesn't match.

Important rule:

The participating SELECT statements should have:

1. The same number of columns

2. Compatible data types in corresponding positions. The column names in the final result generally come from the first SELECT.

6๏ธโƒฃ Column Order Matters

Set operators match columns by position, not by column name. So structure your queries carefully:

SELECT customer_id, customer_name FROM customers
UNION ALL
SELECT customer_id, customer_name FROM archived_customers;


7๏ธโƒฃ ORDER BY with UNION

If you want to sort the final combined result, put "ORDER BY" at the end.

SELECT customer_id, customer_name FROM customers_2025
UNION
SELECT customer_id, customer_name FROM customers_2026
ORDER BY customer_id;


8๏ธโƒฃ INTERSECT

"INTERSECT" returns rows that exist in both result sets.

SELECT customer_id FROM customers_2025
INTERSECT
SELECT customer_id FROM customers_2026;
  • โค 2
More from @sqlanalyst
  1. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  2. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  3. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
  5. Sep 28, 2026๐Ÿง  Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from thโ€ฆ
  6. Sep 28, 2026๐ŸŽ“ ๐—›๐—”๐—ฅ๐—ฉ๐—”๐—ฅ๐—— ๐—จ๐—ก๐—œ๐—ฉ๐—˜๐—ฅ๐—ฆ๐—œ๐—ง๐—ฌ ๐—™๐—ฅ๐—˜๐—˜ ๐—ข๐—ก๐—Ÿ๐—œ๐—ก๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿ˜ Dreaming ofโ€ฆ
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 โ†’