Sub topic 4
UNION ALL: keeping every row
Assume you can write basic SELECT queries and understand tables, NULL, DISTINCT, and ORDER BY. This chapter focuses on combining query results without removing duplicate rows.
UNION ALL is a set operator that appends the rows from one query to the rows from another query. It keeps every row, including rows that have identical values.
SELECT email FROM customers
UNION ALL
SELECT email FROM newsletter_subscribers;The database returns all emails from customers, followed by all emails from newsletter_subscribers. If the same email appears in both tables, it appears twice in the result.
How UNION ALL combines rows
Each query is evaluated first. The database then appends the second result to the first result.
The two queries must be compatible:
- They must return the same number of columns.
- Columns in corresponding positions must have compatible data types.
- The output column names usually come from the first query.
- The column order is determined by the first query.
For example, this query labels the source of each row:
SELECT email, 'customer' AS source
FROM customers
UNION ALL
SELECT email, 'subscriber' AS source
FROM newsletter_subscribers;The first query returns two columns: email and source. The second query must also return two columns in that order. The literal values identify where each row originated.
| source | |
|---|---|
| [email protected] | customer |
| [email protected] | subscriber |
The repeated email is meaningful here. It shows that the person appears in both source tables.
UNION ALL versus UNION
UNION removes duplicate result rows. UNION ALL does not.
| Operator | Duplicate rows | Typical use |
|---|---|---|
| `UNION` | Removes them | Create a distinct combined list |
| `UNION ALL` | Keeps them | Preserve events, counts, or source records |
Suppose each table contains one row for [email protected]. UNION returns one row. UNION ALL returns two rows. If one table contains that email twice, UNION ALL returns all three rows.
UNION ALL is especially useful for combining partitions or monthly tables:
SELECT order_id, ordered_at, total
FROM orders_2026_01
UNION ALL
SELECT order_id, ordered_at, total
FROM orders_2026_02;This query preserves every order from both months. It does not check whether an order_id is repeated. If the same order exists in both tables, both rows remain, which may reveal a data-loading problem or may be intentional.
Ordering the combined result
A combined query has one final result. To sort that result, put one ORDER BY after the final SELECT:
SELECT email, 'customer' AS source
FROM customers
UNION ALL
SELECT email, 'subscriber' AS source
FROM newsletter_subscribers
ORDER BY email, source;Without ORDER BY, do not rely on the order in which rows appear. Even if the database often returns the first query's rows first, that behavior is not a guaranteed presentation order.
NULL values are also preserved. If a query returns a row containing NULL, UNION ALL keeps that row. It does not remove it or treat it as missing input.
Exercise
A customer email appears twice in customers and once in newslettersubscribers. How many rows does this query return for that email? `SELECT email FROM customers UNION ALL SELECT email FROM newslettersubscribers;`
Show answer
It returns three rows. UNION ALL preserves every matching row from both queries.