Monday 19 March 2012

Union and union All in Sql Server


Union vs. Union All Query Syntax

Union vs. Union All SQL Syntax

The purpose of the SQL UNION and UNION ALL commands are to combine the results of two or more queries into a single result set consisting of all the rows belonging to all the queries in the union. The question becomes whether or not to use the ALL syntax.
The main difference between UNION ALL and UNION is that, UNION only selects distinct values, while UNION ALL selects all values (including duplicates).
The syntax for UNION {ALL} is as follows:
[SQL Statement 1]
UNION {ALL}
[SQL Statement 2]

Sample Data

Use Authors table in SQL Server Pubs database or just use a simple table with these values (obviously simplified to just illustrate the point):
CityStateZip
NashvilleTN37215
LawrenceKS66044
CorvallisOR97330

UNION ALL Example

This SQL statement combines two queries to retrieve records based on states. The two queries happen to both get records from Tennessee ('TN'):
SELECT City, State, Zip FROM Authors WHERE State IN ('KS', 'TN')
UNION ALL
SELECT City, State, Zip FROM Authors WHERE IN ('OR' 'TN')

Result of UNION ALL syntax:

CityStateZip
NashvilleTN37215
LawrenceKS66044
NashvilleTN37215
CorvallisOR97330
Notice how this displays the two query results in the order they appear from the queries. The first two records come from the first SELECT statement, and the last two records from the second SELECT statement. The TN record appears twice, since both SELECT statements retrieve TN records.

Union Query SQL Example

Using the same SQL statements and combining them with a UNION command:
SELECT City, State, Zip FROM Authors WHERE State IN ('KS', 'TN')
UNION
SELECT City, State, Zip FROM Authors WHERE IN ('OR' 'TN')

Result of UNION Query

CityStateZip
CorvallisOR97330
LawrenceKS66044
NashvilleTN37215
Notice how the TN record only appears once, even though both SELECT statements retrieve TN records. The UNION syntax automatically eliminates the duplicate records between the two SQL statements and sorts the results. In this example the Corvallis record appears first but is from the second SELECT statement.

Temporary Index Issues for UNION Queries

UNION query, by definition, eliminates all duplicate rows (as opposed to UNION ALL) and is slower. To do this in SQL Server, it must build a temporary index on all the columns returned by both queries. If the index cannot be build for the queries, you will get a SQL error. In this situation, UNION ALL may be the solution if you don't have rights to do this. Alternatively, you may need to use a temporary table with INSERT queries to store the results of each query and use a standard SELECT query to group and get the unique values.

No comments:

Post a Comment