UNION vs UNION ALL in SQL: What’s the Difference and When to Use Each

The difference between UNION and UNION ALL in SQL is mainly about duplicates and performance.

Here’s the breakdown 👇

FeatureUNIONUNION ALL
DuplicatesRemoves duplicate rows from the result set.Keeps all duplicates (does not remove them).
PerformanceSlower, because SQL has to check and remove duplicates using DISTINCT.Faster, because it simply combines results without checking for duplicates.
Use CaseWhen you need unique results from multiple queries.When you need all results, including duplicates.

🔹 Example

Table: Customers_USA

idname
1John
2Mary

Table: Customers_Canada

idname
2Mary
3Alex

Query 1 — Using UNION

SELECT name FROM Customers_USA
UNION
SELECT name FROM Customers_Canada;

Result (no duplicates):

name
John
Mary
Alex

Query 2 — Using UNION ALL

SELECT name FROM Customers_USA
UNION ALL
SELECT name FROM Customers_Canada;

Result (includes duplicates):

name
John
Mary
Mary
Alex

🔹 Summary

  • Use UNION → when you want unique combined results.
  • Use UNION ALL → when you want all rows (and better performance).