What is the difference between UNION
and UNION ALL
?
UNION
merges the contents of two structurally-compatible tables into a single combined table.
The difference between UNION
and UNION ALL
is that UNION will
omit duplicate records whereas UNION ALL
will include duplicate records.
Union
Result set is sorted in ascending order whereas UNION ALL
Result set is not sorted
UNION
performs a DISTINCT
on its Result set so it will eliminate any duplicate rows. Whereas UNION ALL
won't remove duplicates and therefore it is faster than UNION
.*
Note: The performance of UNION ALL
will typically be better than UNION
, since UNION
requires the server to do the additional work of removing any duplicates. So, in cases where it is certain that there will not be any duplicates, or where having duplicates is not a problem, use of UNION ALL
would be recommended for performance reasons.