# How to use SQL UNION

SQL `UNION` **combines records from two or more tables into a target table**, without duplicates. The operator helps you merge related records into one table, enabling a better overview of your data and more efficient queries.

## What is SQL `UNION`?

Whether you’re dealing with customer data, price information, product details, project records or financial data, maintaining complex [databases](https://www.ionos.com/digitalguide/hosting/technical-matters/databases/) always presents a challenge. Part of that challenge is combining records **efficiently into meaningful result tables**. That’s where the SQL `UNION` [operator](https://www.ionos.com/digitalguide/server/configuration/sql-operators/) comes in. You can use `UNION` in your queries, comparisons and analyses to combine selected records from one or more tables and remove duplicates.

It can be used with other [SQL commands](https://www.ionos.com/digitalguide/server/configuration/sql-commands/) and functions like `ORDER BY`, `GROUP BY`, `WHERE` and `HAVING` to create **relevant data structures based on clear criteria**. With aggregate functions like [SQL AVG](https://www.ionos.com/digitalguide/server/configuration/sql-avg/) and [SQL SUM](https://www.ionos.com/digitalguide/server/configuration/sql-sum/) you can combine tables and calculate and sort result sets.

Tip Before you take your first steps with [SQL](https://www.ionos.com/digitalguide/server/know-how/what-is-sql/) you’ll need some knowledge of the basics. Our [introduction to SQL with examples](https://www.ionos.com/digitalguide/server/configuration/sql-introduction-with-examples/) provides a good foundation for getting going in SQL.

## What is the syntax of SQL `UNION`?

The basic syntax for SQL `UNION` looks as follows:

```sql
SELECT  ColumnA, ColumnB, … or *  FROM  TableA
UNION
SELECT ColumnA, ColumnB, … or *  FROM  TableA
```

The following parameters are used:

- `SELECT`: Use [SELECT](https://www.ionos.com/digitalguide/server/configuration/sql-select/) to specify which columns you want to bring together. You can use an asterisk `*` to select all records in the table. You need to select the same number of columns in the same order in each `SELECT` element.
- `UNION`: Combines the selected records from two tables into a single target table.

## What is SQL `UNION` used for?

There is no way around SQL `UNION` if you need to merge related records like customer files, financial data or product information. Here are some use cases for the `UNION` operator in SQL:

- Create complete digital files and folders with customer data, project data, patient information or orders
- Create sales reports or compare data from different departments
- Analyze purchase behavior to generate leads
- Analyze and compare financial and transaction data
- Remove duplicates from reports or merged records
- Evaluate production volumes, shipping quantities or inventories
- Aggregate order and sales data to identify trends or perform marketing analyses

Tip Take advantage of secure storage, scalable performance and individual server and hosting offers with [SQL Server Hosting](https://www.ionos.com/cloud/sql-server-hosting/ "SQL Server Hosting from IONOS") from IONOS. Choose from cloud servers, vServers (VPS), dedicated servers or Linux hosting.

## Examples of SQL UNION

We’ll now look at some examples that show how SQL `UNION` works in practice.

### Combining records into a single table

Let’s say you want to combine the records from two tables called “Subscribers” and “Customers” into a target table without duplicates, using the columns “Name”, “Address” and “CustomerID”. That would look as follows:

```sql
SELECT  Name, Address, CustomerID
FROM  Subscribers
UNION
SELECT  Name, Address, CustomerID
FROM  Customers
```

### Combining employee data from different departments

In this example, you want to combine employee data from tables called “Marketing” and “Sales” using columns like “Name” and “City”. That would look as follows:

```sql
SELECT  Name, City
FROM  Marketing
UNION
SELECT  Name, City
FROM  Sales
```

### Combing records with `UNION ALL`

We’ll now turn to an example where we do not want to exclude duplicates. You can use `UNION ALL` to specify that **a target table should contain all records including duplicates**. For this example, we’ll take two tables named “Customers” and “Suppliers” and make a new target table that includes all customers and suppliers located in Portland.

```sql
SELECT  Name, City
FROM  Customers
WHERE  City  =  'Portland'
UNION ALL
SELECT  Name, City
FROM  Suppliers
WHERE  City  =  'Portland'
GROUP BY  City;
```

## What are the alternatives to SQL `UNION`?

There are several other options for combining data in SQL. Here are some of the most common options:

- `JOIN`: You can use [SQL JOIN](https://www.ionos.com/digitalguide/server/configuration/sql-join/) to merge, query and join data. It includes commands like `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, `FULL JOIN` and `CROSS JOIN`. `JOIN` statements offer you more flexibility and customization than `UNION`, thanks to the option to add `ON` clauses.
- `GROUP BY`: [GROUP BY](https://www.ionos.com/digitalguide/server/configuration/sql-group-by/) allows you to combine selected records with identical values in a target table.
- `ORDER BY`: [ORDER BY](https://www.ionos.com/digitalguide/server/configuration/sql-order-by/) enables you to specify which order you want records to appear in in a target table.
- `SELECT DISTINCT`: [SQL SELECT DISTINCT](https://www.ionos.com/digitalguide/server/configuration/sql-select-distinct/) is similar to `UNION` and returns only unique query results without duplicates in a target table.
- `UNION ALL`: `UNION ALL` combines records much like `UNION` but returns all records, including duplicates.


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/sql-union/](https://www.ionos.com/digitalguide/server/configuration/sql-union/) for AI/LLM consumption.