Detective's Journal
SQL UNION vs UNION ALL: When to Use Each (With Visual Examples)
February 7, 2026
11 min read
Hristo Bogoev

SQL UNION vs UNION ALL: When to Use Each (With Visual Examples)

Learn the key differences between UNION and UNION ALL in SQL. Visual examples showing when duplicates matter, performance tips, and a decision flowchart.

UNION and UNION ALL both combine query results vertically, but one removes duplicates while the other keeps them all. Choosing wrong can tank your query performance or give you incorrect results. This guide shows the difference visually.

UNION vs UNION ALL: The Visual Difference

Before we look at any SQL, let's see what these operators actually do to your data. Imagine you're a detective combining witness lists from two different crime scenes.

πŸ“‹ Your Source Data

Scene A Witnesses:

  • Detective Mills
  • Officer Chen
  • Dr. Lopez

Scene B Witnesses:

  • Detective Mills
  • Officer Park
  • Dr. Lopez

Notice: Detective Mills and Dr. Lopez appear at both scenes.

πŸ”΅UNION Result (4 rows)
  • Detective Mills
  • Officer Chen
  • Dr. Lopez
  • Officer Park

Duplicates removed. Each person listed once.

🟒UNION ALL Result (6 rows)
  • Detective Mills
  • Officer Chen
  • Dr. Lopez
  • Detective Mills
  • Officer Park
  • Dr. Lopez

All rows kept. Duplicates preserved.

That's the core difference. UNION gives you unique rows. UNION ALL gives you everything. The question is: which one do you actually need?

How UNION Works (With Code)

UNION combines the results of two or more SELECT statements and automatically removes duplicate rows. It's essentially doing a DISTINCT on the combined results.

SELECTname, role
Columns must match in both queries
FROMscene_a_witnesses
First data source
UNION
Combines results, removes duplicates
SELECTname, role
Same columns, same order
FROMscene_b_witnesses
Second data source

Anatomy of a UNION query

Example:

-- Combine witness lists, removing duplicates
SELECT name, role FROM scene_a_witnesses
UNION
SELECT name, role FROM scene_b_witnesses;

Result: 4 rows. Detective Mills and Dr. Lopez appear once each, even though they were at both scenes.

Practice combining data from multiple tables in SQLNoir's detective cases, where you'll query witnesses, suspects, and clues across different crime scenes.

How UNION ALL Works (With Code)

UNION ALL combines the results of two or more SELECT statements and keeps all rows, including duplicates. No deduplication means no extra work for the database.

Example:

-- Combine witness lists, keeping ALL rows
SELECT name, role FROM scene_a_witnesses
UNION ALL
SELECT name, role FROM scene_b_witnesses;

Result: 6 rows. All original rows from both tables, including duplicates.

πŸ”When duplicates matter
If a witness appears at multiple crime scenes, that's a clue! UNION ALL preserves this information. UNION would hide it. Sometimes what looks like "duplicate data" is actually valuable insight.

Side-by-Side Comparison

FeatureUNIONUNION ALL
DuplicatesRemovedKept
PerformanceSlower (sorts + dedupes)Faster (no extra step)
Result sizeSmaller or equalAlways sum of both
When to useNeed unique rowsNeed all rows or know no dupes exist
Memory usageHigher (sorting)Lower

UNION vs UNION ALL at a Glance

When to Use UNION vs UNION ALL

The decision is simpler than most tutorials make it. Start with this flowchart:

πŸ”€Combining query results?
πŸ€”Could duplicates exist across sources?
⚠️Would duplicates cause incorrect results?
πŸ”΅Use UNION (removes dupes)

If you answered NO to either question, use UNION ALL (faster)

In practice, UNION ALL is the right choice more often than you'd think. Here are the key scenarios:

βœ… Use UNION ALL when:

  • β€’ Your sources can't have overlapping rows (e.g., transactions from different months)
  • β€’ You want to count duplicates (e.g., tracking how many times a person was mentioned)
  • β€’ Performance matters and you know duplicates aren't an issue
  • β€’ You're combining data and will deduplicate later anyway

πŸ”΅ Use UNION when:

  • β€’ You need a unique list (e.g., all customers who bought product A OR product B)
  • β€’ Duplicates would skew your results (e.g., counting or summing)
  • β€’ You're not sure if sources overlap and correctness beats performance

Performance: Why UNION ALL is Usually Faster

UNION has hidden work. To remove duplicates, the database must sort all rows and compare them. On small datasets this is negligible. On millions of rows, it can make a real difference.

Example: Non-overlapping data sources

-- Active and closed cases CAN'T overlap (mutually exclusive)
-- Using UNION here wastes database resources
SELECT case_id, title FROM active_cases
UNION ALL  -- ← Correct: skip unnecessary deduplication
SELECT case_id, title FROM closed_cases;

A case is either active or closed, never both. UNION would sort everything for no benefit.

⚠️Performance trap
Using UNION "just to be safe" when your data can't have duplicates wastes database resources. Know your data before choosing.
Case File #003πŸ”

The Miami Marina Murder

●●○Intermediate

Think you understand how to combine data from multiple sources? Put your detective skills to the test.

Start Investigation

Common Mistakes (And How to Avoid Them)

These are the errors that trip up most SQL users when working with UNION operations.

Mistake 1: Column count mismatch

❌Error: Column count mismatch
SELECT name, role, badge_number FROM officers
UNION ALL
SELECT name, role FROM detectives;

β€’First query has 3 columns, second has 2

β€’SQL will throw an error

βœ…Fixed: Match column count with NULL
SELECT name, role, badge_number FROM officers
UNION ALL
SELECT name, role, NULL AS badge_number FROM detectives;

β€’Both queries now have 3 columns

β€’NULL fills the missing data

Every SELECT in a UNION must have the same number of columns

Mistake 2: ORDER BY in the wrong place

❌Error: ORDER BY before UNION
SELECT name FROM suspects ORDER BY name
UNION ALL
SELECT name FROM witnesses;

β€’ORDER BY can only appear once, at the very end

β€’Most databases will throw a syntax error

βœ…Correct: ORDER BY at the end
SELECT name FROM suspects
UNION ALL
SELECT name FROM witnesses
ORDER BY name;

β€’ORDER BY applies to the combined result

β€’Sorts all rows together

ORDER BY comes after all UNION operations, and only once

Mistake 3: Using UNION when UNION ALL is correct

The scenario:

-- Combining January and February transactions
-- Each transaction has a unique ID - duplicates IMPOSSIBLE

-- ❌ Wasteful: UNION sorts and checks for duplicates that can't exist
SELECT * FROM january_transactions
UNION
SELECT * FROM february_transactions;

-- βœ… Better: UNION ALL skips unnecessary work
SELECT * FROM january_transactions
UNION ALL
SELECT * FROM february_transactions;

When you know duplicates are impossible, UNION ALL is always the right choice.

Mistake 4: Incompatible data types

❌Error: Type mismatch
SELECT name, hire_date FROM employees
UNION ALL
SELECT name, salary FROM contractors;

β€’hire_date is a DATE, salary is a NUMBER

β€’Can't combine incompatible types in same position

βœ…Fixed: Separate columns for different types
SELECT name, hire_date, NULL AS salary FROM employees
UNION ALL
SELECT name, NULL AS hire_date, salary FROM contractors;

β€’Each data type gets its own column

β€’NULL fills gaps appropriately

Columns at the same position must have compatible data types

UNION vs JOIN: What's the Difference?

This confusion is common. UNION and JOIN both combine data, but in completely different ways.

AspectUNION / UNION ALLJOIN
DirectionVertical (stacks rows)Horizontal (adds columns)
Result shapeSame columns, more rowsMore columns, varies rows
RequirementsSame column count & typesRelated key columns
Use caseCombine similar datasetsConnect related datasets

UNION stacks data vertically, JOIN connects data horizontally

Visual example:

-- UNION: Stack two lists vertically
-- Result: More rows, same columns
SELECT name FROM team_a    -- 3 rows
UNION ALL
SELECT name FROM team_b;   -- 3 rows = 6 total rows

-- JOIN: Connect tables horizontally
-- Result: Same(ish) rows, more columns
SELECT a.name, b.department
FROM employees a
JOIN departments b ON a.dept_id = b.id;

Think of UNION as stacking two spreadsheets on top of each other. JOIN is like adding columns from one spreadsheet next to another based on a matching key.

Test Your Knowledge

πŸ” Detective’s Quiz

Q1.You need to combine two tables with identical structure. One is 'current_employees' and one is 'former_employees'. Some people might appear in both (rehires). You want a list of all unique people. Which do you use?

Q2.You're combining sales data from Q1 and Q2 tables. Each row is a distinct transaction with a unique ID. No transaction can appear in both tables. Which is faster?

Q3.You run: SELECT name FROM tableA UNION ALL SELECT name FROM tableB. TableA has 100 rows, tableB has 50 rows. How many rows in the result?

Q4.Which requires more database resources to execute?

Ready to put your SQL skills to work on real mysteries?

SQLNoir's 6 detective cases challenge you to write queries, combine clues, and crack cases using real SQL.

Start Your Investigation β†’

FAQ

What is the difference between UNION and UNION ALL in SQL?

UNION combines results and removes duplicate rows. UNION ALL combines results and keeps all rows including duplicates. UNION ALL is faster because it skips the deduplication step.

When should I use UNION instead of UNION ALL?

Use UNION when you need unique rows and duplicates would be incorrect for your use case. Use UNION ALL when you need all rows, or when you know your source data cannot have overlapping rows (which is the more common scenario).

Which is faster, UNION or UNION ALL?

UNION ALL is faster because it simply appends results. UNION must sort and compare all rows to find and remove duplicates, which takes extra CPU time and memory.

Can I use ORDER BY with UNION?

Yes, but only once at the very end of your combined query. ORDER BY applies to the final combined result, not to individual SELECT statements. Putting ORDER BY before UNION will cause a syntax error.

What happens if the columns don't match in a UNION?

You'll get an error. Both SELECT statements must have the same number of columns, and the data types must be compatible. You can use NULL or type casting to align columns when needed.

Ready to start your next investigation?

Jump into the SQLNoir case files and put these tips to work.