MySQL UNION
MySQL UNION Operator
To merge the result-set of two or more SELECT statements, use the UNION operator.
- Within UNION, each SELECT statement needs to have an equal number of columns.
- Similar data types must also be present in the columns.
- Additionally, each SELECT statement’s columns must have the same order.
UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL Syntax
By default, the UNION operator chooses only unique values. Use UNION ALL to allow duplicate values.
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Note: Typically, the column names in the first SELECT query and the column names in the result-set match.
Demo Database
We’ll be using the well-known Northwind sample database in this tutorial.
A sample from the “Customers” table is shown below:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
And a selection from the “Suppliers” table:
| SupplierID | SupplierName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Exotic Liquid | Charlotte Cooper | 49 Gilbert St. | London | EC1 4SD | UK |
| 2 | New Orleans Cajun Delights | Shelley Burke | P.O. Box 78934 | New Orleans | 70117 | USA |
| 3 | Grandma Kelly’s Homestead | Regina Murphy | 707 Oxford Rd. | Ann Arbor | 48104 | USA |
SQL UNION Example
The “Customers” and “Suppliers” tables’ cities (only unique values) are returned using the SQL statement that follows:
Example
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Note: Since UNION only chooses unique values, cities that are shared by a number of suppliers or customers will only be mentioned once. To additionally choose duplicate values, use UNION ALL!
SQL UNION ALL Example
The cities (including duplicate entries) from the “Customers” and “Suppliers” tables are returned using the SQL query that follows:
Example
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
SQL UNION With WHERE
The “Customers” and “Suppliers” tables’ German cities (only separate values) are returned using the SQL statement that follows:
Example
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
SQL UNION ALL With WHERE
The German cities (including duplicate entries) from the “Customers” and “Suppliers” tables are returned by the SQL query that follows:
Example
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Another UNION Example
All suppliers and customers are listed in the SQL statement that follows:
Example
SELECT 'Customer' AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;
Take note of the “AS Type”—an alias—above. A table or column can have a temporary name assigned to it using SQL aliases. An alias is only present while the query is running. In this case, we have a temporary column called “Type” that indicates if the individual in touch is a “Supplier” or a “Customer”.