0% found this document useful (0 votes)
51 views3 pages

Union Vs Union All

The UNION command selects distinct values from two tables with columns of the same data type, while UNION ALL selects all values including duplicates. The document demonstrates the difference between UNION and UNION ALL using sample tables and queries. UNION performs an additional DISTINCT SORT operation to eliminate duplicates, while UNION ALL simply concatenates the results without sorting. There are three rules for UNION: it must contain two or more SELECT statements separated by UNION; each query must have matching columns in the same order; and column data types must be compatible.

Uploaded by

y_raj20
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Union Vs Union All

The UNION command selects distinct values from two tables with columns of the same data type, while UNION ALL selects all values including duplicates. The document demonstrates the difference between UNION and UNION ALL using sample tables and queries. UNION performs an additional DISTINCT SORT operation to eliminate duplicates, while UNION ALL simply concatenates the results without sorting. There are three rules for UNION: it must contain two or more SELECT statements separated by UNION; each query must have matching columns in the same order; and column data types must be compatible.

Uploaded by

y_raj20
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UNION

The UNION command is used to select related information from two tables, much like
the JOIN command. However, when using the UNION command all selected columns
need to be of the same data type. With UNION, only distinct values are selected.

UNION ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL
selects all values.

Run following script to produce result for UNION and UNION ALL.
/* Create First Table */
DECLARE @Table1 TABLE (Col INT)
INSERT INTO @Table1
SELECT 1
INSERT INTO @Table1
SELECT 2
INSERT INTO @Table1
SELECT 3
INSERT INTO @Table1
SELECT 4
INSERT INTO @Table1
SELECT 5

/* Create Second Table */


DECLARE @Table2 TABLE (Col INT)
INSERT INTO @Table2
SELECT 1
INSERT INTO @Table2
SELECT 2
INSERT INTO @Table2
SELECT 6
INSERT INTO @Table2
SELECT 7
INSERT INTO @Table2
SELECT 8

/* Result of Union operation */


SELECT Col 'Union'
FROM @Table1
UNION
SELECT Col
FROM @Table2

/* Result of Union All operation */


SELECT Col 'UnionAll'
FROM @Table1
UNION ALL
SELECT Col
FROM @Table2
GO

The difference between Union and Union all is that 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.
A UNION statement effectively does a SELECT DISTINCT on the results set. If you
know that all the records returned are unique from your union, use UNION ALL
instead, it gives faster results.

If you look at the resultset it is clear that UNION ALL gives result unsorted but in
UNION result are sorted. Let us see the query plan to see what really happens when this
operation are done.
From the plan it is very clear that in UNION clause there is an additional operation of
DISTINCT SORT takes place where as in case of UNION ALL there is no such
operation but simple concatenation happens. From our understanding of UNION and
UNION ALL this makes sense.

There are three rules of UNION one should remember.

UNION RULES

 A UNION must be composed of two or more SELECT statements, each separated


by the keyword UNION.
 Each query in a UNION must contain the same columns, expressions, or
aggregate functions, and they must be listed in the same order.
 Column datatypes must be compatible: They need not be the same exact same
type, but they must be of a type that SQL Server can implicitly convert.

You might also like