UNION and UNION ALL Operators in SQL Server(TSQL)

 

With UNION and UNION ALL Opertors, we can merge multiple sql statement’s result in a single result set.

When we merge two SQL statements with the UNION operator, duplicate records are listed once in the result set.

When we merge two SQL statements with the UNION ALL operator, all results are listed.

If there are no duplicate records in queries that we will merge with union or union all, UNION and UNION ALL will return the same result set. We will explain the difference in performance between them at the end of the article.

UNION or UNION ALL operators are used when we need to merge multiple result sets in SQL Server. For example, let’s assume that the internet sales and the store sales are in different tables in a database. To list the total sales, a combination of these two tables is required. When using UNION and UNION ALL operators, the following points should be considered.

  • The number of columns in all result sets must be the same.
  • The data types of the columns corresponding to each other must match each other.

Examples about UNION and UNION ALL:

First, let’s create 2 sample tables.

First, let’s see the difference between UNION and UNION ALL:

UNION ALL:

UNION:

As seen in this example, when we use UNION, the recurring records are listed 1 times in the result set.

When we try to merge two result sets with a different number of columns, we get an error as follows.

Msg 205, Level 16, State 1, Line 1

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

Let’s change the column order in Query. Because the data types are the same, we will not encounter any errors in UNION operation.

If we change the query as follows, we get an error when we run the query. Because the data types of the ID and Date columns do not match.

Msg 206, Level 16, State 2, Line 1

Operand type clash: int is incompatible with date

Let’s run the UNION query by taking only the year parts of the date columns.

We said that if there are no duplicate records in the two queries, UNION and UNION ALL will bring the same result.

So how are the performance values of UNION and UNION ALL?

The following two query will return the same result set. Because there is no duplicate record. When we examine the execution plans, the cost of the query is almost tripled as the Distinct Sort operation is done in the UNION process. Therefore, if you are sure that there are no duplicate records, you must use UNION ALL.