What is the difference between UNION and UNION ALL, and when would you prefer one over the other?

Last updated 3 July 2026

  • Both combine query results with matching column counts and compatible types. UNION removes duplicates, while UNION ALL keeps every row.

  • UNION must sort or hash to deduplicate, so UNION ALL is faster and cheaper.

  • Prefer UNION ALL unless 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
All SQL interview questions