SQL Server UNION

Summary: in this tutorial, you will learn how to use the SQL Server UNION to combine the results of two or more queries into a single result set.

Introduction to SQL Server UNION operator #

SQL Server UNION is one of the set operations that allow you to combine results of two SELECT statements into a single result set which includes all the rows that belong to the SELECT statements in the union.

The following illustrates the syntax of the SQL Server UNION:

query_1
UNION
query_2
Code language: SQL (Structured Query Language) (sql)

The following are requirements for the queries in the syntax above:

  • The number and the order of the columns must be the same in both queries.
  • The data types of the corresponding columns must be the same or compatible.

The following Venn diagram illustrates how the result set of the T1 table unions with the result set of the T2 table: