TExcelTAD
Home › SQL › SQL UNION

SQL UNION

UNION stacks the results of two or more SELECT statements on top of each other into one result set.

sql
SELECT columns FROM table1
UNION
SELECT columns FROM table2;

Rules:

  • Every SELECT must return the same number of columns.
  • Corresponding columns should have compatible types.
  • Column names come from the first SELECT.
  • ORDER BY is written once, at the very end, and sorts the combined result.

Cities of customers and suppliers

sql · try it yourself
SELECT city, country FROM customers WHERE country IN ('UK', 'Norway', 'USA')
UNION
SELECT city, country FROM suppliers WHERE country IN ('UK', 'Norway', 'USA')
ORDER BY country, city;
city      | country
----------+--------
Oslo      | Norway
Edinburgh | UK
London    | UK
Austin    | USA
New York  | USA
(5 rows)

Oslo and Austin appear in both tables but only once in the result: UNION removes duplicate rows.

UNION ALL keeps duplicates

UNION ALL simply appends the rows. It is faster, because the database does not need to look for duplicates:

sql · try it yourself
SELECT city FROM customers WHERE country IN ('Norway', 'USA')
UNION ALL
SELECT city FROM suppliers WHERE country IN ('Norway', 'USA')
ORDER BY city;
city
--------
Austin
Austin
New York
Oslo
Oslo
(5 rows)

Tip: Use UNION ALL unless you really want duplicates removed. It is cheaper and does not surprise you by merging rows that just happen to be equal.

Labelling the source

Add a constant column to tell where each row came from:

sql · try it yourself
SELECT 'customer' AS type, name, country FROM customers WHERE country = 'Japan'
UNION ALL
SELECT 'supplier', name, country FROM suppliers WHERE country = 'Japan'
UNION ALL
SELECT 'employee', first_name || ' ' || last_name, NULL FROM employees WHERE employee_id = 8
ORDER BY type, name;
type     | name          | country
---------+---------------+--------
customer | Code & Coffee | Japan
employee | Hana Sato     | NULL
supplier | Volt Gadgets  | Japan
(3 rows)

Building a small report

UNION ALL can add a total row under a grouped result:

sql · try it yourself
SELECT status, COUNT(*) AS orders, 1 AS sort_key
FROM orders
GROUP BY status
UNION ALL
SELECT 'TOTAL', COUNT(*), 2
FROM orders
ORDER BY sort_key, status;
status     | orders | sort_key
-----------+--------+---------
cancelled  | 7      | 1
delivered  | 16     | 1
processing | 8      | 1
shipped    | 9      | 1
TOTAL      | 40     | 2
(5 rows)

INTERSECT and EXCEPT

Two siblings of UNION compare result sets instead of stacking them:

Operator Returns
A UNION B rows in A or B (duplicates removed)
A UNION ALL B all rows of A, then all rows of B
A INTERSECT B rows in both A and B
A EXCEPT B rows in A but not in B (Oracle: MINUS)
sql · try it yourself
SELECT country FROM customers
INTERSECT
SELECT country FROM suppliers
ORDER BY country;
country
-------
France
India
Japan
Norway
Spain
UK
USA
(7 rows)

MySQL supports INTERSECT and EXCEPT only since version 8.0.31.

Test yourself

What is the difference between UNION and UNION ALL?

  1. UNION ALL removes duplicates
  2. UNION removes duplicates; UNION ALL keeps them
  3. UNION only works on two tables
  4. There is none

Where do the column names of a UNION result come from?

  1. The last SELECT
  2. The first SELECT
  3. Both are merged
  4. They are numbered

Which operator returns countries that have customers but no suppliers?

  1. UNION
  2. INTERSECT
  3. EXCEPT
  4. UNION ALL