Basic about UNION ALL Operator – using and example

This SQL tutorial explains how to use the SQL UNION ALL operator with syntax and examples.

Description

The SQL UNION ALL operator is used to combine the result units of two or greater SELECT statements. It does not remove duplicate rows between the quite a number SELECT statements (all rows are returned).

Each SELECT statement within the UNION ALL must have the identical quantity of fields in the end result units with comparable data types.

What is the distinction between UNION and UNION ALL?

UNION gets rid of duplicate rows. UNION ALL does no longer cast off duplicate rows.

Syntax

The syntax for the UNION ALL operator in SQL is:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION ALL
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Parameters or Arguments

expression1, expression2, expression_n The columns or calculations that you want to retrieve. tables The tables that you wish to retrieve documents from. There ought to be at least one desk listed in the FROM clause. WHERE conditions Optional. The conditions that need to be met for the records to be selected.

Note

There must be equal variety of expressions in each SELECT statements The corresponding expressions need to have the equal data kind in the SELECT statements. For example: expression1 should be the identical records kind in each the first and 2nd SELECT statement. See additionally the UNION operator.

Example – Single Field With Same Name

Let’s look at how to use the SQL UNION ALL operator that returns one field. In this easy example, the subject in each SELECT statements will have the equal identify and statistics type.

For example:

SELECT supplier_id
FROM suppliers
UNION ALL
SELECT supplier_id
FROM orders
ORDER BY supplier_id;

This SQL UNION ALL instance would return the supplier_id more than one instances in the end result set if that identical value seemed in both the suppliers and orders table. The SQL UNION ALL operator does not get rid of duplicates. If you want to cast off duplicates, try the use of the UNION operator.

Now, let’s explore this instance further will some data.

If you had the suppliers desk populated with the following records:

supplier_id supplier_name 1000 Microsoft 2000 Oracle 3000 Apple 4000 Samsung

And the orders table populated with the following records:

order_id order_date supplier_id 1 2015-08-01 2000 2 2015-08-01 6000 3 2015-08-02 7000 4 2015-08-03 8000

And you done the following UNION ALL statement:

SELECT supplier_id
FROM suppliers
UNION ALL
SELECT supplier_id
FROM orders
ORDER BY supplier_id;

You would get the following results:

supplier_id 1000 2000 2000 3000 4000 6000 7000 8000

As you can see in this example, the UNION ALL has taken all supplier_id values from both the suppliers desk as well as the orders table and returned a combined result set. No duplicates were eliminated as you can see with the aid of the supplier_id value of 2000 appearing twice in the end result set.

Example – Different Field Names

It is not vital that the corresponding columns in every SELECT declaration have the identical name, however they do want to be the equal corresponding records types.

When you don’t have the identical column names between the SELECT statements, it gets a bit tricky, in particular when you prefer to order the results of the question the use of the ORDER BY clause.

Let’s seem at how to use the UNION ALL operator with unique column names and order the query results.

For example:

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
UNION ALL
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 1;

In this SQL UNION ALL example, for the reason that the column names are extraordinary between the two SELECT statements, it is more fantastic to reference the columns in the ORDER BY clause by their function in the result set. In this example, we have sorted the results by using supplier_id / company_id in ascending order, as denoted by using the ORDER BY 1. The supplier_id / company_id fields are in function #1 in the end result set.

Now, let’s explore this example in addition with data.

If you had the suppliers desk populated with the following records:

supplier_id supplier_name 1000 Microsoft 2000 Oracle 3000 Apple 4000 Samsung

And the agencies desk populated with the following records:

company_id company_name 1000 Microsoft 3000 Apple 7000 Sony 8000 IBM

And you finished the following UNION ALL statement:

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
UNION ALL
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 1;

You would get the following results:

supplier_id supplier_name 3000 Apple 3000 Apple 4000 Samsung 7000 Sony 8000 IBM

First, notice that the document with supplier_id of 3000 appears twice in the end result set because the UNION ALL question returns all rows and does no longer remove duplicates.

Second, word that the column headings in the result set are referred to as supplier_id and supplier_name. This is because these were the column names used in the first SELECT assertion in the UNION ALL.

If you had wanted to, you should have aliased the columns as follows:

SELECT supplier_id AS ID_Value, supplier_name AS Name_Value
FROM suppliers
WHERE supplier_id > 2000
UNION ALL
SELECT company_id AS ID_Value, company_name AS Name_Value
FROM companies
WHERE company_id > 1000
ORDER BY 1;

Now the column headings in the end result will be aliased as ID_Value for the first column and Name_Value for the 2d column.

ID_Value Name_Value 3000 Apple 3000 Apple 4000 Samsung 7000 Sony 8000 IBM