Skip to content
Justice Tech School

UNION

UNION combines the result of two SELECT statements into one list and removes duplicates. UNION ALL keeps duplicates and is faster. Both queries must return the same number of columns with compatible types.

Example
SELECT country FROM students WHERE id <= 3
UNION
SELECT 'Egypt'
ORDER BY country;

Result

country
Egypt
Ghana
Nigeria
Example
SELECT name FROM students WHERE country = 'Ghana'
UNION ALL
SELECT name FROM students WHERE score > 85;

Result

name
Kofi
Esi
Ada
Zainab

Exercise

Keep duplicates when combining results.

SELECT a FROM t1 UNION  SELECT a FROM t2;

Quiz Test

What does UNION do with duplicate rows?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.