Pages

Wednesday 3 April 2013

Difference between union and union all in SQL-Server- 2008?

UNION 

·         UNION is used to select distinct values from two tables.
·         Union are slow as compare to Union ALL
·         UNION similar to JOIN command.
·         When using the UNION command all selected columns need to be of the same data type.

Example
SELECT 'TEST'
      UNION
SELECT 'TEST'

RESULT:
 TEST


UNION ALL
·         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.
·         UNION ALL is faster

Example
SELECT 'TEST'
      UNION ALL
SELECT 'TEST'

RESULT:
TEST
TEST

No comments:

Post a Comment