UNION vs UNION ALL: Stacking Two Query Results

UNION and UNION ALL both stack the results of two SELECT queries on top of each other, as if you pasted one list under another. The difference is duplicates: UNION removes rows that appear more than once, while UNION ALL keeps every row exactly as it came back. Because removing duplicates takes extra work, UNION ALL is usually faster, so use it whenever duplicates are impossible or you actually want them. Both need the two queries to return the same number of columns, in the same order, with compatible types.

A tiny example

Imagine a school club app with two small tables, one for members and one for guests who signed up for a single event:

members                 guests
id | email              id | email
1  | ana@example.org    1  | sam@example.org
2  | ben@example.org    2  | ben@example.org

You want one mailing list from both tables.

SELECT email FROM members
UNION
SELECT email FROM guests;

This returns three rows: ana, ben and sam. Ben is in both tables, but UNION shows him once.

SELECT email FROM members
UNION ALL
SELECT email FROM guests;

This returns four rows, with ben twice. Nothing is checked or removed. You can try both in your own practice database; how to create your first SQLite database gets you set up in a few minutes.

Picture it as stacking, not joining

Beginners often mix up UNION and JOIN, because both combine two tables. The difference is direction:

JOINUNION / UNION ALL
DirectionSide by side: adds columnsOn top of each other: adds rows
NeedsA matching column, such as a keyThe same number and kind of columns
Typical question"Show each order with its customer's name""Show one list of everyone from two sources"

If joins are still new, our page that compares INNER, LEFT, RIGHT and FULL JOIN is a good companion. It also shows a place where UNION appears in real life: MySQL has no FULL OUTER JOIN, so people build one from a LEFT JOIN and a RIGHT JOIN combined with UNION.

The rules both versions follow

  1. Same number of columns. If the first query returns two columns, the second must too.
  2. Same order of meaning. Columns are matched by position, not by name. Putting email, name in one query and name, email in the other mixes them up without any error.
  3. Compatible types. Text with text, numbers with numbers. Some databases convert types for you, others refuse; see understanding SQL data types.
  4. Names come from the first query. The result's column names are taken from the first SELECT, so put your aliases there.
  5. One ORDER BY, at the end. Sorting applies to the whole stacked result and goes after the last query.
SELECT email, 'member' AS source FROM members
UNION ALL
SELECT email, 'guest' FROM guests
ORDER BY email;

Adding a source column like this is a handy trick: it labels where each row came from. Note that it also changes duplicate removal, because ben-as-member and ben-as-guest are now different rows.

What counts as a duplicate?

UNION compares whole rows. Two rows are duplicates only if every column matches. Text comparison may or may not ignore upper and lower case, depending on the database and its settings, so Ben@example.org and ben@example.org might count as different. When duplicates matter, it is often clearer to clean the values first, for example with LOWER(email). Rows with NULL in the same column are treated as duplicates of each other by UNION, which is different from how NULL behaves in a WHERE comparison; our page NULL explained covers that odd value.

Two cousins: INTERSECT and EXCEPT

SQLite, PostgreSQL and SQL Server support both. Recent MySQL versions do too, while older MySQL versions do not, so check yours. You can often write the same thing with EXISTS or NOT EXISTS subqueries, as in subqueries and CTEs for beginners.

When to reach for UNION in a real app

Most beginner apps need set operations less often than joins, but a few situations come up again and again:

If you find yourself writing the same UNION over and over, you can save it with a name as a view, so the app can query it like a table.

Common mistakes

Frequently asked questions

Is UNION ALL faster than UNION?

Usually, yes. UNION has to find and remove duplicate rows, which takes extra work. UNION ALL simply returns everything.

Do the column names have to match?

No. Columns are matched by position, and the result uses the names from the first query. The number of columns and their types must match.

Can I use ORDER BY with UNION?

Yes, once, at the very end. It sorts the whole combined result.

Find the next lesson in the beginner hub.