softwaretips4u Tips that Transform
โ† Back to Homepage โ† Back to Articles

SQL UNION vs UNION ALL

๐Ÿ”น What They Do

UNION and UNION ALL combine result sets produced by two or more SELECT statements. Both require matching column counts and compatible data types so the rows line up correctly.

๐Ÿ”น 1. UNION

SELECT city FROM customers
UNION
SELECT city FROM suppliers;
โœ… Result: only the unique city names from both tables.

๐Ÿ”น 2. UNION ALL

SELECT city FROM customers
UNION ALL
SELECT city FROM suppliers;
โœ… Result: every city value, including duplicates.

๐Ÿ”น 3. Rules for Both

๐Ÿ”น Example Tables

Customers
idcity
1New York
2London
Suppliers
idcity
1London
2Paris

๐Ÿ”น UNION Result

New York
London
Paris

๐Ÿ”น UNION ALL Result

New York
London
London
Paris

๐Ÿ”น When to Use

โ† Back to Articles | ๐Ÿ  Back to Homepage