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:
| JOIN | UNION / UNION ALL | |
|---|---|---|
| Direction | Side by side: adds columns | On top of each other: adds rows |
| Needs | A matching column, such as a key | The 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
- Same number of columns. If the first query returns two columns, the second must too.
- Same order of meaning. Columns are matched by position, not by name. Putting
email, namein one query andname, emailin the other mixes them up without any error. - Compatible types. Text with text, numbers with numbers. Some databases convert types for you, others refuse; see understanding SQL data types.
- Names come from the first query. The result's column names are taken from the first
SELECT, so put your aliases there. - 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
INTERSECTreturns rows found in both queries: "people who are members and also signed up as guests".EXCEPTreturns rows from the first query that are not in the second: "guests who are not members yet". Oracle calls itMINUS.
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:
- One list from similar tables. Members and guests, current and archived orders, or posts and comments shown in one activity feed.
- Adding a summary row. A list of categories with counts, plus one extra row for the total, built as a second query.
- Combining searches. Results that match a name, stacked with results that match a tag, with duplicates removed.
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
- Using
UNIONby habit whenUNION ALLwould do, which makes the database do needless work. - Using
UNION ALLand being surprised by duplicates in a mailing list. - Putting
ORDER BYinside the first query instead of at the end. - Mismatching column order between the two queries.
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.