Sub topic 3
UNION: merging results and removing duplicates
Prerequisites and the basic idea
This chapter assumes that you can write SELECT queries with WHERE, understand tables and rows, and use DISTINCT and ORDER BY. You should also know that NULL represents a missing or unknown value.
A set operator combines the result sets of two or more queries. UNION appends the rows from the second query to the rows from the first query, then removes duplicate rows.
For example, suppose both tables contain an email column:
SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers;The result contains every distinct email found in either table. An email found in both tables appears once.
Making two queries compatible
The queries on both sides of UNION must be union-compatible. This means:
- They must return the same number of columns.
- Corresponding columns must have compatible data types.
- The column names in the final result usually come from the first query.
- The column order matters.
This query is compatible because both queries return one text column:
SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers;This query is also compatible. Both sides return two columns, and the second column is text:
SELECT email, 'customer' AS source
FROM customers
UNION
SELECT email, 'subscriber' AS source
FROM newsletter_subscribers;The literal values make the source visible in the result. Because source differs, two rows with the same email may no longer be duplicates.
| source | |
|---|---|
| [email protected] | customer |
| [email protected] | subscriber |
A common error is selecting different numbers of columns:
-- Error: the two SELECT statements return different numbers of columns
SELECT email, customer_id
FROM customers
UNION
SELECT email
FROM newsletter_subscribers;You can make it compatible by adding a placeholder with a compatible type:
SELECT email, customer_id
FROM customers
UNION
SELECT email, NULL AS customer_id
FROM newsletter_subscribers;Some databases need an explicit type cast for the NULL placeholder.
Duplicate removal
UNION removes duplicate result rows across the entire combined result. It does not remove duplicates based only on one column unless that is the only column you select.
SELECT email, 'customer' AS source
FROM customers
UNION
SELECT email, 'subscriber' AS source
FROM newsletter_subscribers;If the same email occurs once with each source value, the rows are different. Both remain.
This behavior is similar to applying DISTINCT after combining the results:
SELECT DISTINCT email
FROM (
SELECT email FROM customers
UNION ALL
SELECT email FROM newsletter_subscribers
) AS all_emails;However, write direct UNION when that expresses your intent. It is clearer and lets the database optimize the operation.
Ordering the combined result
Place one ORDER BY after the final query. It sorts the complete result, rather than sorting each input separately.
SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers
ORDER BY email;In many databases, the final ORDER BY must use a column name or position available in the first query:
SELECT email, 'customer' AS source
FROM customers
UNION ALL
SELECT email, 'subscriber' AS source
FROM newsletter_subscribers
ORDER BY email;UNION is different from UNION ALL. UNION removes duplicates, while UNION ALL keeps every input row and usually avoids the work needed to compare and remove duplicates.
Exercise
If the same email appears in both input queries, will SELECT email ... UNION SELECT email ... return it once or twice?
Show answer
It will return the email once because UNION removes duplicate result rows.