loading

SQL Union

The SQL UNION Operator

To merge the result-set of two or more SELECT statements, use the UNION operator.

  • Every SELECT statement within UNION must have the same number of columns
  • The columns must also have similar data types
  • The columns in every SELECT statement must also be in 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;
				
			
Share this Doc

SQL Union

Or copy link

Explore Topic