UNION
MySQL UNION
operator allows you to combine two or more result sets of queries into a single result set. The following illustrates the syntax of the UNION
operator:
To combine result set of two or more queries using the UNION
operator, these are the basic rules that you must follow:
First, the number and the orders of columns that appear in all
SELECT
statements must be the same.Second, the data types of columns must be the same or compatible.
By default, the UNION
operator removes duplicate rows even if you don’t specify the DISTINCT
operator explicitly.
Let’s see the following sample tables: t1
and t2
:
The following statement combines result sets returned from t1
and t2
tables:
The final result set contains the distinct values from separate result sets returned by the queries:
Because the rows with value 2 and 3 are duplicates, the UNION
removed them and kept only unique values.
The following Venn diagram illustrates the union of two result sets that come from t1
and t2
tables:
If you use the UNION ALL
explicitly, the duplicate rows, if available, remain in the result. Because UNION ALL
does not need to handle duplicates, it performs faster than UNION DISTINCT
.
As you can see, the duplicates appear in the combined result set because of the UNION ALL
operation.
UNION
vs. JOIN
UNION
vs. JOIN
A JOIN
combines result sets horizontally, a UNION
appends result set vertically. The following picture illustrates the difference between UNION
and JOIN
:
MySQL UNION
and column alias examples
UNION
and column alias examplesWe’ll use the customers
and employees
tables in the sample database for the demonstration:
Suppose that you want to combine the first name and last name of employees and customers into a single result set, you can use the UNION
operator as follows:
As you can see from the output, the MySQL UNION
uses the column names of the first SELECT
statement for the column headings of the output.
If you want to use other column headings, you need to use column aliases explicitly in the first SELECT
statement as shown in the following example:
This example uses the column heading of the first query for the output. It uses the CONCAT()
function to concatenate first name, space, and last name into a full name.
MySQL UNION
and ORDER BY
UNION
and ORDER BY
If you want to sort the result set of a union, you use an ORDER BY
clause in the last SELECT
statement as shown in the following example:
Notice that if you place the ORDER BY
clause in each SELECT
statement, it will not affect the order of the rows in the final result set.
To differentiate between employees and customers, you can add a column as shown in the following query:
MySQL also provides you with an alternative option to sort a result set based on column position using ORDER BY
clause as follows:
However, it is not a good practice to sort the result set by column position.
In this tutorial, you have learned how to use MySQL UNION
statement to combine data from multiple queries into a single result set.
Last updated