Skip to content
Common mistakes/SQL: UNION ALL Keeps Duplicate Rows
Common mistake

SQL: UNION ALL Keeps Duplicate Rows

UNION ALL appends every row exactly as returned. Use UNION only when duplicate rows should collapse.

The symptom

You combine two queries, then notice duplicate rows in the result. The query is not broken: UNION ALL is doing exactly what it promises by stacking both result sets without deduplication.

Why it happens

UNION ALL is an append operation. If the same row appears in both inputs, it appears twice in the output. That is useful for audit logs, event streams, and any report where duplicate rows represent real activity.

It is wrong when the question is about unique entities, such as one row per customer, product, page, or date.

The fix

Use UNION when duplicate result rows should be removed, and keepUNION ALL only when a repeated row stands for something that really happened twice. Both queries below stack the customers behind shipped Garden Shop orders onto the customers who used a coupon — two filters over the sameorders table, so the lists overlap.

Wrong: UNION ALL keeps duplicates
select customer_id
from orders
where status = 'shipped'
union all
select customer_id
from orders
where coupon_code is not null
order by customer_id
Right: UNION deduplicates
select customer_id
from orders
where status = 'shipped'
union
select customer_id
from orders
where coupon_code is not null
order by customer_id

Theo Brandt, customer 1, accounts for four of the appended rows on his own: two shipped orders, plus two orders carrying a coupon code, one of which is also one of the shipped ones. UNION collapses the stack to the 14 distinct customers behind it. When you want that collapse but also want control over what else the query does, keep UNION ALL and deduplicate explicitly with distinct or a grouping query.

Choose based on meaning

UNION removes duplicate rows after both inputs are combined.UNION ALL is usually faster and preserves all rows, but that is only correct when repeats are meaningful.

Learn more