4.6. SET OPERATIONS

4.6.1. SET Operations — UNION , UNION ALL

4.6.1.1. UNION Operator

UNION Operator combines the results of two or more queries into a single result set consisting of all the rows belonging to all queries in the union. This is different from using joins that combine columns from two tables.

Two basic rules for combining the result sets of two queries with UNION are as follows.

The number and the order of the columns must be identical in all queries.

The data types must be compatible. See Table 4-8.

Syntax
{< query specification > | ( < query expression > ) }
UNION [ ALL ]
< query specification | ( < query expression > )
[ UNION [ ALL ] < query specification | ( < query expression > )
[ ...n ] ]
Arguments
< query_specification ...

Get Transact-SQL Desk Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.