Monday, October 01, 2012

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

Posted by Rahul Kharde at 9:37 AM

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


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top