---
title: "What is the purpose of the UNION operator in SQL and how does it work?"  
description: "What is the purpose of the UNION operator in SQL and how does it work?"  
author: "Revati S Misra"  
published: 2023-06-30  
updated: 2023-11-18  
canonical: https://www.mindstick.com/forum/158911/what-is-the-purpose-of-the-union-operator-in-sql-and-how-does-it-work  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the purpose of the UNION operator in SQL and how does it work?

What is the [purpose](https://yourviews.mindstick.com/view/247/no-fail-policy-failing-its-purpose) of the [UNION](https://www.mindstick.com/articles/1510/union-example-in-sql-server) [operator in SQL](https://www.mindstick.com/forum/160543/how-to-filter-records-using-like-operator-in-sql-server) and how does it work?

## Replies

### Reply by Aryan Kumar

The UNION [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) is used to combine the result sets of two or more SELECT statements into a single result set. The purpose of the UNION operator is to merge rows from different tables or queries while removing duplicate rows from the final result set. Each SELECT statement within the UNION must have the same number of columns in the result sets, and the corresponding columns must have compatible data types.

Here's a basic syntax of the UNION operator:

```plaintext
SELECT column1, column2, ...
FROM table1
WHERE condition
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition;
```

Key points about the UNION operator:

## Columns and Data Types:

- The SELECT statements within a UNION must have the same number of columns, and the corresponding columns must have compatible data types.

## Duplicates Removal:

- The UNION operator removes duplicate rows from the result set. If you want to include duplicate rows, you can use the UNION ALL operator.

## Ordering:

- The order of rows in the final result set is not guaranteed to be in any specific order unless you use the ORDER BY clause at the end of the last SELECT statement.

## NULL Handling:

- The UNION operator treats NULL values in columns as equal. If a column in one SELECT statement has a NULL value and the corresponding column in another SELECT statement has a non-NULL value, they are considered duplicates and only one of them will appear in the result set.

Here's a simple example to illustrate the use of the UNION operator:

```plaintext
SELECT employee_id, employee_name FROM employees
WHERE department = 'HR'
UNION
SELECT employee_id, employee_name FROM employees
WHERE department = 'IT';
```

In this example, the UNION operator is used to combine the results of two SELECT statements. The result will include distinct employee records from both the HR and IT departments.

It's worth noting that the UNION operator is different from the JOIN operation, which is used to combine rows from two or more tables based on a related column between them. UNION, on the other hand, is used to combine rows from the result sets of two or more SELECT statements.


---

Original Source: https://www.mindstick.com/forum/158911/what-is-the-purpose-of-the-union-operator-in-sql-and-how-does-it-work

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
