What is the difference between UNION and UNION ALL?

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

What is the difference between UNION and UNION ALL?

26条回答
  •  无人共我
    2020-11-21 11:55

    Both UNION and UNION ALL concatenate the result of two different SQLs. They differ in the way they handle duplicates.

    • UNION performs a DISTINCT on the result set, eliminating any duplicate rows.

    • UNION ALL does not remove duplicates, and it therefore faster than UNION.

    Note: While using this commands all selected columns need to be of the same data type.

    Example: If we have two tables, 1) Employee and 2) Customer

    1. Employee table data:

    enter image description here

    1. Customer table data:

    enter image description here

    1. UNION Example (It removes all duplicate records):

    enter image description here

    1. UNION ALL Example (It just concatenate records, not eliminate duplicates, so it is faster than UNION):

    enter image description here

提交回复
热议问题