Skip to content
Hands-on Mentor
ENTR
Back to blog

UNION vs UNION ALL in SQL: the difference, with examples

UNION vs UNION ALL in SQL: UNION removes duplicate rows, UNION ALL keeps every row and runs faster. See both on real query results.

4 min read

The difference between UNION and UNION ALL in SQL is how they treat duplicate rows. UNION stacks the results of two queries and removes duplicates. UNION ALL stacks them and keeps every row, including duplicates. Because it skips the duplicate check, UNION ALL is also faster.

The examples use the student table of the Northbridge University database in Port Alder (120 students). Two lists are enough: students with a scholarship (28) and fourth-year students (21).

What does UNION do?

UNION runs both queries, puts the rows one under the other, and drops any row that appears more than once. Here are the cities of both groups:

SELECT city FROM student WHERE has_scholarship
UNION
SELECT city FROM student WHERE year = 4
ORDER BY city;
city
Cairo
Lagos
Lisbon
Madrid
Manila
Mumbai
Nairobi
Port Alder
Seoul
Toronto
Warsaw

The two queries return 28 and 21 rows, but the result has only 11 rows: each city shows up once. Without ORDER BY, SQL does not promise any order for the rows.

What does UNION ALL do?

UNION ALL keeps every row from both queries. Nothing is compared or removed:

SELECT city, COUNT(*) AS n
FROM (
  SELECT city FROM student WHERE has_scholarship
  UNION ALL
  SELECT city FROM student WHERE year = 4
) AS both_lists
GROUP BY city
ORDER BY n DESC, city
LIMIT 3;
cityn
Port Alder15
Manila6
Madrid5

The subquery returns 28 + 21 = 49 rows. Because duplicates stay, you can count them. With UNION in the same place, every city would count as 1.

Why can UNION give you fewer rows than you expect?

UNION compares whole rows, not single columns. Two rows are duplicates only if every selected column matches. Compare the row counts when you select the student id too:

SELECT id, first_name, last_name FROM student WHERE has_scholarship
UNION ALL
SELECT id, first_name, last_name FROM student WHERE year = 4;

This returns 49 rows. Change UNION ALL to UNION and it returns 44 rows. The 5 missing rows are students who are in both lists: they have a scholarship and are in year 4. UNION kept each of them once.

This is the main danger. If you want a total or a count, a quiet duplicate removal gives you a wrong number.

Which one is faster?

UNION ALL is faster or equal. To remove duplicates, the database must sort or hash all rows and compare them. UNION ALL skips that work. On small tables you will not notice. On large tables the difference grows.

When should you use UNION and when UNION ALL?

SituationUse
The two queries cannot overlap (for example, different years or different tables)UNION ALL
You need a total or a count and every row mattersUNION ALL
You want a list of distinct values from two sourcesUNION
You are not sureCheck the overlap first, then choose

A good habit is to write UNION ALL by default, and use UNION only when you know that duplicates are possible and unwanted.

Here is a typical case for UNION ALL: a small summary report that puts three counts in one result. The three queries cannot overlap, because each one is a different label:

SELECT 'scholarship' AS list, COUNT(*) AS students FROM student WHERE has_scholarship
UNION ALL
SELECT 'fourth year', COUNT(*) FROM student WHERE year = 4
UNION ALL
SELECT 'both', COUNT(*) FROM student WHERE has_scholarship AND year = 4;
liststudents
scholarship28
fourth year21
both5

What rules must both queries follow?

Both queries need the same number of columns, and the columns in the same position need compatible types. The column names come from the first query. Two errors show up often:

SELECT first_name FROM student
UNION
SELECT last_name, first_name FROM instructor;
Outputeach UNION query must have the same number of columns
SELECT id, first_name FROM student
UNION
SELECT first_name, last_name FROM instructor;
OutputUNION types integer and character varying cannot be matched

In the second query, id is a number and first_name is text. Fix both errors by selecting matching columns in the same order.

What about INTERSECT and EXCEPT?

They belong to the same family of set operations. INTERSECT returns the rows that appear in both queries. EXCEPT returns the rows of the first query that are not in the second.

Using the student id: scholarship students who are also in year 4 (INTERSECT) are 5. Scholarship students who are not in year 4 (EXCEPT) are 23. Both remove duplicates, like UNION.

What to read next

  • Practice in your browser: [Set operations](/learn/sql/set-operations)
  • Related post: [SQL JOIN types](/blog/sql-join-types), the other way to combine tables
  • Remove or find missing values: [SQL NULL values](/blog/sql-null-values)
  • Browse the whole course: [SQL course](/courses/sql)
  • #sql
  • #union
  • #union all
  • #set operations
All posts