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

  • Sanjib Kumar Maji
    March 23, 2007 11:48 pm

    Very good for understanding difference between UNION Vs UNION ALL

    Reply
  • Union command does 2 things
    1) It works like distinct clause ie it removes duplicate rows also it shows the rows which are unique in a particular table.
    eg if Table A has rows with values 1,2,3 and Table B has rows with values 4,2,3
    the Union table A and table B would give result
    1,2,3,4

    Reply
  • this is really a good article to understand but can u give this example with a example. This will be the good for all the people to understand the concept very clearly.

    Thanks………..

    Reply
  • Another example, which this article helped me troubleshoot today:

    First Table: 1,2,2,3
    Second Table: 4,5

    Union All:
    1,2,2,3,4,5

    Union:
    1,2,3,4,5

    I suppose I understood that union would remove duplicates between the two tables, but I didn’t realize that it would also remove the EXISTING duplicates (which I wanted to keep!).

    Reply
    • declare @t1 table(i int)
      insert into @t1 select 1 union all select 2 union all select 2 union all select 3
      declare @t2 table(i int)
      insert into @t2 select 3 union select 1 union select 4 union select 5
      select * from @t1 as t

      where exists(
      select i from @t1
      where i=t.i
      group by i having count(*)>1
      )

      union all
      (
      select * from @t1 as t
      where exists(
      select i from @t1
      where i=t.i
      group by i having count(*)=1
      )
      union
      select i from @t2
      )

      Reply
  • always union all have the better performance.

    Reply
  • Hi all,
    I want to ask if i can use union ineasted of join,please show examples

    Reply
  • Wilson Gunanithi . J
    June 12, 2007 3:46 pm

    Hi Pin ,

    With the help of your statement about UNION and UNION ALL , I ve learnt interesting matters of difference between them .

    Thanks a lot.

    Regards,

    Wilson Gunanithi . J

    Reply
  • Great post…. very easy to understand….keep up the work…

    Reply
  • Hi all,

    How to eliminate the duplicate rows using union all functions.

    Reply
  • Hi,
    Really it was help full for me.

    Thnx

    Reply
  • Its really good article to understand… the diff. between both UNION and UNION ALL..:)

    Reply
  • Hi,

    Its realy helpful for me.

    Thanks

    Reply
  • Very good for understanding difference between UNION Vs UNION ALL.
    I work as a software Engineer in CFCS. I develop more than 10 web site through ASP and SQL.
    Ravi Ranjan

    Reply
  • Mohammed Siddiq.K
    September 26, 2007 12:25 am

    hi pinaldave, this is very useful informaton for us

    Reply
  • Good explanation….

    I have got one problem… I would appreciate if any one help me out solve the problem…

    Here is my requirement. I have to write a store procedure which returns a table like below.

    create procedure temp
    AS
    BEGIN

    if(condition1)
    BEGIN
    sql statement1
    END

    if(condition2)
    BEGIN
    sql statement2
    END

    END

    Now i have to combined these two tables and have to return. How can i use UNION or UNION all here in this case???

    I hope the requirement is clear…

    Reply
    • You can write this

      create procedure temp
      AS
      BEGIN

      sql statement1 where condition1
      union all
      sql statement2 where condition2

      END

      Reply
  • hi

    super explaination i understand very clearly

    Reply
  • EXCELLENT THANKS VERY MUCH

    Reply
  • Hi Suresh,

    For your example of having to effectively union two statements that are nested in different IF statements, you may want to use a temp table / table variable.

    if(condition1)
    BEGIN
    INSERT INTO #MyTable
    SELECT * FROM Table1
    END

    if(condition2)
    INSERT INTO #MyTable
    SELECT * FROM Table2
    END

    SELECT * FROM #MyTable

    In the above example, you would need to explicitly decide if you wanted to do a simple SELECT (UNION ALL) or SELECT DISTINCT (UNION).

    Also, you should carefully choose to use a temp table (#tmp) or table variable (@tmp). There are a number of differences, but basically, variable are better for small resultsets and temp tables better for large. When using a variable table, you lose all parallelism, so inserting into the table can take significantly longer.

    Reply
    • create procedure temp
      AS
      BEGIN

      sql statement1 where condition1
      union all
      sql statement2 where condition2

      END

      Reply
  • hemant inamdar
    January 8, 2008 10:31 am

    Great!!!! Keep It Up!!!

    Reply
  • I would like to work with u….

    If u can recognize me i am vaibhav pathak…….

    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 2005………

    Reply

Leave a Reply