SQL SERVER – Union vs. Union All – Which is better for performance?

This article is completely re-written with better example SQL SERVER – Difference Between Union vs. Union All – Optimal Performance Comparison. I suggest all of my readers to go here for update article.

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.

Example:
Table 1 : First,Second,Third,Fourth,Fifth
Table 2 : First,Second,Fifth,Sixth

Result Set:
UNION: First,Second,Third,Fourth,Fifth,Sixth (This will remove duplicate values)
UNION ALL: First,First,Second,Second,Third,Fourth,Fifth,Fifth,Sixth,Sixth (This will repeat values)

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Download, SQL Scripts
Previous Post
SQL SERVER – Download 2005 SP2a
Next Post
SQL SERVER – DBCC command to RESEED Table Identity Value – Reset Table Identity

Related Posts

190 Comments. Leave new

  • can u tell me what is ‘sysobject’ in Sql Server 2005 ??

    Regards Vaibhav

    Reply
  • I would like to work with u…. If u allow !!!!!!!!

    If u can recognize me i am vaibhav pathak……. I have acd interview taken by u.

    I like ur article for difference beteen Union and Union All

    You have selected me but the boss of ur company had no faith on me thats why i did not come to ur company on 15th jan 2008………

    Reply
  • dear all,
    union all is always best performance

    Reply
  • thx its good

    Reply
  • Dear Pinal Dave,

    Pls send me ms sqldba certification question & answere

    Reagrds
    Adhi

    Reply
  • Nice article. Keep it up Pinal

    Reply
  • Virendra Bhati
    March 26, 2008 8:22 pm

    I read many question from that site.
    Every one was good and very near to the target.
    So I really love that site and now always go through that.

    Reply
  • Hello,

    I have a challange here Im hoping someone could help… I got two tables with identical schema that need to be “glued” together, but the duplicate records must be truncated under certain condiction:

    For example, if the PK field is identical in both tables, I would want the actDate to be evaluated and the one with higher value to be selected during the UNION process… how can I accomplish this?

    Thanks a whole bunch in advance!!

    Reply
  • hi Pinal
    Good article
    i have a doubt
    what if if the table contains a Null value
    can u give example in this regard

    With thanks
    sree

    Reply
  • If I have use a UNION ALL with two selects why does the order change instead of returning the results in natural order? For example if I have SELECT 0 As SortOrder FROM XXX UNION ALL SELECT 1 As SortOrder FROM AAA the 1 will be the first record instead of the second (some times)? If I add a Sort Order to the entire SQL Statement then my query slows down to much since it is buried in a bunch of other queries.

    Reply
  • Amit Mhatre
    May 4, 2008 3:10 pm

    Hi Pinal,

    The article of Union v/s Union ALL was fantastic, it clears the doubts of difference between UNION AND UNION ALL.

    My only qus is : Does Union ALL also shows repeated columns (duplicates) where the datatype is common, which is the case in UNION clause.

    Reply
  • Hi Pinal,

    I am a beginer in SQL and have a query and finding difficult solving this. Can you please help me.

    [Northwind] List the CompanyName, ContactName, City, Region, PostalCode, Country, and Phone of all Customers who have ordered product ‘Tofu’ and of the Supplier of that product. Use UNION. Show all output rows.

    Thanks
    Zainab

    Reply
  • Thanks Pinal,

    Union and Union All descreption in your words is very straight-forward and easy to understand.

    Reply
  • Muhammad Wasim
    July 1, 2008 4:34 pm

    pinaldave,

    Is the any difference in UNION and UNION ALL sorting order?
    As in case of UNION SQL Server build temporay indexes on all the columns.

    Thanks
    Muhammad Wasim

    Reply
  • Muhammad Wasim
    July 1, 2008 4:35 pm

    correction!!
    As in case of UNION SQL Server build temporay index (not indexes) on all the columns.

    Reply
  • Sanjay Sharma
    July 2, 2008 1:46 pm

    Nice article to understand the difference b/w UNION and UNION ALL.

    Keep it up :)

    Reply
  • soutik dutta
    July 7, 2008 7:27 pm

    Very comprehensive stuff. Great.

    Reply
  • hi ,
    its reakky a good article . well i wana know how can i import data from excel sheet into 2005 database.pls do reply

    thanks
    amit sharma

    Reply
  • Hi amit,

    you can import the Excel Sheet data using the Sql Server Import and Export Command. Below is the steps for the same:
    1. Right click on the Database Select Tasks-> Import Data…
    2. Select the datasource as ‘Microfost Excel’
    3. Browse the location of the Excel File.
    4. if you want to Import into the already existing table then you can Map the columns otherwise you can import into the new tables too.

    Its is very simple wizard.

    Please do let me know if you still face any issue.

    Rupesh

    Reply
  • hi Rupesh,

    thanks for urs reply, well rupesh i am still getting error while importing the excel file.

    error:
    Error 0xc00470fe: Data Flow Task: The product level is insufficient for component “Source – Assessment$” (1).
    (SQL Server Import and Export Wizard)

    may i know wht is this error and what i have to do for resolving this error

    thanks
    amit sharma

    Reply

Leave a Reply