What is the difference between UNION and UNION ALL, and when would you prefer one over the other?
Last updated
Both combine query results with matching column counts and compatible types.
UNIONremoves duplicates, whileUNION ALLkeeps every row.UNIONmust sort or hash to deduplicate, soUNION ALLis faster and cheaper.Prefer
UNION ALLunless you genuinely need a distinct result set.
- https://motherduck.com/glossary/union-and-union-all/
- https://oneuptime.com/blog/post/2026-03-31-mysql-mysql-union-vs-union-all-differences-and-performance/view
- https://medium.com/@workwithalam/why-you-should-use-union-all-instead-of-union-in-sql-b10420766d50
- https://prachub.com/resources/union-vs-union-all
- https://medium.com/@tiye_78900/union-vs-union-all-the-sql-difference-thats-worth-a-few-extra-seconds-2d18b39e3c36
- https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql?view=sql-server-ver17
- https://www.postgresql.org/docs/current/queries-union.html
- https://medium.com/@bigtechinterviews/union-vs-union-all-in-sql-a-comprehensive-guide-bc83a453d448