SQLite UNION Operator
The UNION Operator
UNION combines the results of two or more SELECT statements into one result.
- Every
SELECTmust have the same number of columns - The columns should have similar data types
UNIONremoves duplicate rows. To keep them, use UNION ALL
This example returns the cities that have customers or suppliers, each city only once:
Example
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Try it Yourself »
Note: The column names in the result come from the first SELECT.
UNION With WHERE
Example
Select the German cities that have customers or suppliers:
SELECT City, Country FROM Customers
WHERE Country = 'Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country = 'Germany'
ORDER BY City;
Try it Yourself »
Add a Column That Shows the Source
This example lists all the customers and suppliers, with a column that tells which is which:
Example
SELECT 'Customer' AS Type, ContactName, City
FROM Customers
UNION
SELECT 'Supplier', ContactName, City
FROM Suppliers;
Try it Yourself »