What is the difference between UNION and UNION ALL?

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

What is the difference between UNION and UNION ALL?

相关标签:
26条回答
  • 2020-11-21 11:53

    (From Microsoft SQL Server Book Online)

    UNION [ALL]

    Specifies that multiple result sets are to be combined and returned as a single result set.

    ALL

    Incorporates all rows into the results. This includes duplicates. If not specified, duplicate rows are removed.

    UNION will take too long as a duplicate rows finding like DISTINCT is applied on the results.

    SELECT * FROM Table1
    UNION
    SELECT * FROM Table2
    

    is equivalent of:

    SELECT DISTINCT * FROM (
        SELECT * FROM Table1
        UNION ALL
        SELECT * FROM Table2) DT
    

    A side effect of applying DISTINCT over results is a sorting operation on results.

    UNION ALL results will be shown as arbitrary order on results But UNION results will be shown as ORDER BY 1, 2, 3, ..., n (n = column number of Tables) applied on results. You can see this side effect when you don't have any duplicate row.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-21 11:57

    One more thing i would like to add-

    Union:- Result set is sorted in ascending order.

    Union All:- Result set is not sorted. two Query output just gets appended.

    0 讨论(0)
  • 2020-11-21 11:58

    Suppose that you have two table Teacher & Student

    Both have 4 Column with different Name like this

    Teacher - ID(int), Name(varchar(50)), Address(varchar(50)), PositionID(varchar(50))
    

    Student- ID(int), Name(varchar(50)), Email(varchar(50)), PositionID(int)
    

    You can apply UNION or UNION ALL for those two table which have same number of columns. But they have different name or data type.

    When you apply UNION operation on 2 tables, it neglects all duplicate entries(all columns value of row in a table is same of another table). Like this

    SELECT * FROM Student
    UNION
    SELECT * FROM Teacher
    

    the result will be

    When you apply UNION ALL operation on 2 tables, it returns all entries with duplicate(if there is any difference between any column value of a row in 2 tables). Like this

    SELECT * FROM Student
    UNION ALL
    SELECT * FROM Teacher
    

    Output

    Performance:

    Obviously UNION ALL performance is better that UNION as they do additional task to remove the duplicate values. You can check that from Execution Estimated Time by press ctrl+L at MSSQL

    0 讨论(0)
  • 2020-11-21 12:00

    UNION
    The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. With UNION, only distinct values are selected.

    UNION ALL
    The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.

    The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.

    A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题