Difference Between Union and Union All

 Posted by Sathya4260 on 3/1/2011 | Category: Sql Server Interview questions | Views: 6668 | Points: 40
Answer:

Union:
1.UNION only selects distinct values
2.Output is in sorted order

UnionAll:
1.UNION ALL selects all values (including duplicates).
2.Output is not in sorted order

EX:
SELECT Col 

FROM @Table1
UNION
SELECT Col
FROM @Table2


/* Result of Union All operation */
SELECT Col 

FROM @Table1
UNION ALL
SELECT Col
FROM @Table2


Output:
Union:
1
2
3
5

UnionAll:

1
2
3
2
5


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response