What is the difference between UNION and UNION ALL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
UNION and UNION ALL both combine result sets from multiple SELECT statements. The key difference is that UNION removes duplicate rows from the combined result (performing an implicit DISTINCT), while UNION ALL keeps all rows including duplicates. Because UNION must sort and deduplicate, it is slower than UNION ALL. Use UNION ALL when you know there are no duplicates or when duplicates are acceptable.
Basic Syntax
Both require the same number of columns in each SELECT, and the columns must have compatible data types.
UNION (Removes Duplicates)
UNION compares entire rows. Two rows are considered duplicates only if all column values match.
UNION ALL (Keeps Duplicates)
Performance Difference
UNION must perform a sort or hash operation to identify and remove duplicates:
For large result sets, the difference is significant. If table_a returns 1 million rows and table_b returns 1 million rows, UNION must sort all 2 million rows to find duplicates. UNION ALL simply returns all 2 million rows immediately.
When to Use Each
Use UNION ALL when:
Use UNION when:
Combining More Than Two Queries
ORDER BY and LIMIT with UNION
ORDER BY and LIMIT apply to the entire combined result, not to individual SELECT statements:
Column Name and Type Rules
UNION vs JOIN
Common Pitfalls
- Using UNION when UNION ALL suffices: If the source tables are already disjoint (e.g., partitioned by region or date),
UNIONwastes time sorting and deduplicating rows that have no duplicates. Default toUNION ALLand only useUNIONwhen deduplication is explicitly needed. - Column count mismatch: Every
SELECTin aUNIONmust return the same number of columns. If one query returns 3 columns and another returns 4, the query fails. AddNULLas a placeholder column if needed. - ORDER BY on individual SELECTs: Placing
ORDER BYinside one of theSELECTstatements (without a subquery) either causes an error or is ignored by the optimizer. UseORDER BYafter the lastUNIONto sort the final result. - NULL handling in UNION:
UNIONtreats twoNULLvalues as equal when deduplicating, so rows that differ only in havingNULLin the same column are considered duplicates. This may remove rows you intended to keep. - Implicit type casting: If column types differ between
SELECTstatements, the database performs implicit casting which can cause data loss (e.g., truncating aVARCHAR(100)toVARCHAR(50)) or unexpected results. Explicitly cast columns to matching types.
Summary
UNIONremoves duplicates from the combined result (likeSELECT DISTINCTon the final output)UNION ALLkeeps all rows including duplicates and is faster- Use
UNION ALLby default; switch toUNIONonly when deduplication is required - Both require matching column counts and compatible types across all
SELECTstatements ORDER BYandLIMITapply to the entire combined result, not to individual queries
Related reading
- What is the difference between utf8mb4 and utf8 charsets in MySQL?
- what is the disadvantages of database sequencing with machine name + table alias + sequence?
- What is the easiest way to ignore a JPA field during persistence?
- what is the effect of distributed_group_by_no_merge
- What is the error Every derived table must have its own alias in MySQL?
- What is the ideal data type to use when storing latitude / longitude in a MySQL database?
- What is the LIMIT clause alternative in JPQL?
- What is the maximum length of a sql in clickhouse?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.