Thursday, March 5, 2009

Difference b/w union and union all

Often people forget or misunderstand the difference between UNION and the UNION ALL keywords in a query.
UNION ALL
SELECT NOW = GETDATE()
UNION ALL
SELECT NOW = GETDATE()
This gives us the entire 'set' of data, both queries are executed, note, they are executed serially and not in parallel, the order the queries are executed is not determined; so, you never get a situation where the top query is parallelised with the bottom query; the query components themselves may be executed in parallel and combined as the last step.
Output (notice there are two rows that are identical, this is because GETDATE() gives a consistent value across the entire query rather than consistency at each query within the UNION construct.

NOW-----------------------2006-06-29 08:01:48.937
-----------------------2006-06-29 08:01:48.937
For Detail View : http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/29/849.aspx

No comments:

Post a Comment

Earn Money ! Affiliate Program
Open Directory Project at dmoz.org