What is the difference between UNION and UNION ALL?

后端 未结 26 2888
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 11:28

What is the difference between UNION and UNION ALL?

26条回答
  •  执笔经年
    2020-11-21 12:16

    UNION merges the contents of two structurally-compatible tables into a single combined table.

    • Difference:

    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.

提交回复
热议问题