What is the difference between UNION and UNION ALL?

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

What is the difference between UNION and UNION ALL?

26条回答
  •  逝去的感伤
    2020-11-21 12:00

    Just to add my two cents to the discussion here: one could understand the UNION operator as a pure, SET-oriented UNION - e.g. set A={2,4,6,8}, set B={1,2,3,4}, A UNION B = {1,2,3,4,6,8}

    When dealing with sets, you would not want numbers 2 and 4 appearing twice, as an element either is or is not in a set.

    In the world of SQL, though, you might want to see all the elements from the two sets together in one "bag" {2,4,6,8,1,2,3,4}. And for this purpose T-SQL offers the operator UNION ALL.

提交回复
热议问题