UNION combines the result sets of two or more SELECT statements into one result and removes duplicate rows. UNION ALL does the same but keeps every row, which makes it faster. In MySQL, use UNION ALL unless you specifically need duplicates removed.
Both operators stack results vertically, one query after another. That is different from a join, which places columns from different tables side by side. This guide shows the syntax, the rules the queries must follow, and how to sort and filter a combined result.
The examples use two tables that hold people from different sources. Run this in MySQL 8.4 or newer to follow along.
Ravi and Meera appear in both tables. Kiran is only a subscriber and Asha is only a customer.
The basic form is two or more SELECT statements joined by the keyword UNION.
Ravi and Meera appear once even though both tables contain them. UNION compares entire rows and keeps only one copy of each identical row. To do that, MySQL builds a temporary table with a unique key over all selected columns, which costs memory and time.
UNION ALL skips the duplicate check and returns every row from every query.
Six rows come back instead of four. Because MySQL does not need to compare rows, UNION ALL can stream results without building a deduplication table. If you know the queries cannot overlap, for example because they read different date ranges, always choose UNION ALL.
| Operator | Duplicates | Speed | Typical use |
|---|---|---|---|
UNION (same as UNION DISTINCT) | Removed | Slower, needs a temporary table | Merge lists where overlap is expected |
UNION ALL | Kept | Faster | Append disjoint data, logs, partitions |
Every SELECT in a union must satisfy a few rules, or MySQL raises an error.
SELECT.This query fails because the column counts differ:
To fix it, add a placeholder column to the shorter query. A literal NULL or a constant string works.
Because matching is positional, putting columns in the wrong order silently mixes data, such as a name ending up under an email column. Always write the column lists explicitly instead of using SELECT *.
An ORDER BY at the end of the last query applies to the whole union, not to that query alone.
The order column must be a name or position from the first query's column list. You can also write ORDER BY 1.
To sort or limit one branch before combining, wrap that branch in parentheses.
Without the parentheses, an ORDER BY inside a branch is ignored unless it is paired with LIMIT.
A common pattern adds a constant column so you can tell which query each row came from. With UNION ALL, this also keeps rows that would otherwise look identical.
Note that adding source to a plain UNION would stop it from removing duplicates, because the rows now differ in that column.
MySQL 8.0.31 added INTERSECT and EXCEPT. INTERSECT returns only rows present in both queries, and EXCEPT returns rows from the first query that are missing from the second. They follow the same column rules as UNION.
On older servers, use a join or EXISTS to get the same effect.
Yes, usually. UNION has to compare rows and remove duplicates, which needs a temporary table, while UNION ALL just appends results.
Yes. Only the number of columns and their types must be compatible. The final column names come from the first SELECT, and you can rename columns with AS.
UNION stacks rows from separate queries into one list of the same shape. A JOIN combines columns from related tables into wider rows based on a matching condition.
No. Without ORDER BY on the whole union, the order of rows is not defined, even if it looks stable in testing.
UNION merges the results of several SELECT statements and removes duplicate rows, while UNION ALL keeps every row and is faster. Each query must return the same number of columns of compatible types, matched by position. Apply ORDER BY and LIMIT after the last query to affect the whole result, or use parentheses to shape individual branches. Prefer UNION ALL whenever duplicates are impossible or acceptable.