MySQL SQL

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:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1

Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico

And a selection from the “Suppliers” table:

SupplierIDSupplierNameContactNameAddressCityPostalCodeCountry
1Exotic LiquidCharlotte Cooper49 Gilbert St.LondonEC1 4SDUK
2New Orleans Cajun DelightsShelley BurkeP.O. Box 78934New Orleans70117USA
3Grandma Kelly’s HomesteadRegina Murphy707 Oxford Rd.Ann Arbor48104USA

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”.

Share this Doc

MySQL UNION

Or copy link

Explore Topic