BigQuery · Query syntax
BigQuery UNION ALL and UNION DISTINCT
UNION ALL stacks the rows of two queries; UNION DISTINCT does the same and removes duplicates. BigQuery requires the ALL or DISTINCT keyword, matches columns by position unless you add BY NAME, and also offers INTERSECT DISTINCT and EXCEPT DISTINCT.
Set operators combine the results of two queries with the same number of columns. UNION ALL returns every row of both inputs, duplicates included, and is the cheap default. UNION DISTINCT returns each distinct row once, which needs an extra deduplication step. INTERSECT DISTINCT returns the distinct rows present in both inputs, and EXCEPT DISTINCT the distinct rows of the first input that are not in the second. Unlike PostgreSQL, MySQL or Snowflake, BigQuery has no bare UNION: writing it fails with "Expected keyword ALL or keyword DISTINCT". INTERSECT and EXCEPT exist only in their DISTINCT form; INTERSECT ALL is not supported.
Columns are matched by position, not by name. The result takes the column names of the first query, and each pair of columns must have the same type or a common supertype: INT64 and FLOAT64 become FLOAT64, while INT64 and STRING fail with "Column 1 in UNION ALL has incompatible types". Two queries that list the same columns in a different order are combined silently and wrongly. UNION ALL BY NAME matches columns by name and requires the same set of names on both sides. FULL UNION ALL BY NAME keeps columns that exist on only one side and fills the gaps with NULL, LEFT UNION ALL BY NAME keeps the first query's columns, and INNER UNION ALL BY NAME keeps only the shared ones. BY NAME ON (col, ...) picks and orders the output columns, and CORRESPONDING is an equivalent older spelling.
Different set operators cannot be mixed without parentheses: a UNION ALL b UNION DISTINCT c is a syntax error, while a UNION ALL (b UNION DISTINCT c) works. An ORDER BY or LIMIT after the last query applies to the combined result. Every operator except UNION ALL compares rows, so ARRAY, JSON and GEOGRAPHY columns can't go through UNION DISTINCT, INTERSECT or EXCEPT. Do not confuse EXCEPT DISTINCT with SELECT * EXCEPT, which removes columns.
Syntax
query_expr
[{ INNER | FULL [OUTER] | LEFT [OUTER] }]
{ UNION { ALL | DISTINCT } | INTERSECT DISTINCT | EXCEPT DISTINCT }
[BY NAME [ON (column[, ...])] | [STRICT] CORRESPONDING [BY (column[, ...])]]
query_exprArguments and return value
| Argument | Description |
|---|---|
UNION ALL | All rows of both inputs, no deduplication. |
UNION DISTINCT | Rows of both inputs, each distinct row once. |
INTERSECT DISTINCT | Distinct rows that appear in both inputs. |
EXCEPT DISTINCT | Distinct rows of the first input that do not appear in the second. |
BY NAME | Match columns by name instead of position. Prefix FULL, LEFT or INNER to allow different column sets; add ON (columns) to choose the output columns. |
Returns: One result set with the column names of the first input and the common supertype of each column pair.
Examples
UNION ALL keeps duplicates, UNION DISTINCT removes them
WITH web AS (
SELECT 'ana@example.com' AS email UNION ALL SELECT 'ben@example.com'
),
app AS (
SELECT 'ana@example.com' AS email UNION ALL SELECT 'cleo@example.com'
)
SELECT
(SELECT COUNT(*) FROM (SELECT email FROM web UNION ALL SELECT email FROM app)) AS union_all_rows,
(SELECT COUNT(*) FROM (SELECT email FROM web UNION DISTINCT SELECT email FROM app)) AS union_distinct_rows;| union_all_rows | union_distinct_rows |
|---|---|
| 4 | 3 |
INTERSECT and EXCEPT: who signed up on both platforms, who only on the web
WITH web AS (
SELECT 'ana@example.com' AS email UNION ALL SELECT 'ben@example.com'
),
app AS (
SELECT 'ana@example.com' AS email UNION ALL SELECT 'cleo@example.com'
)
SELECT 'both' AS segment, email FROM (SELECT email FROM web INTERSECT DISTINCT SELECT email FROM app)
UNION ALL
SELECT 'web only', email FROM (SELECT email FROM web EXCEPT DISTINCT SELECT email FROM app)
ORDER BY segment;| segment | |
|---|---|
| both | ana@example.com |
| web only | ben@example.com |
Positional matching versus BY NAME
The second query lists its columns in a different order. Plain UNION ALL puts the amount into order_id and the ID into amount; BY NAME lines them up.
SELECT 1001 AS order_id, 50 AS amount
UNION ALL
SELECT 75 AS amount, 1002 AS order_id
UNION ALL
(SELECT 1003 AS order_id, 20 AS amount
UNION ALL BY NAME
SELECT 90 AS amount, 1004 AS order_id)
ORDER BY order_id;| order_id | amount |
|---|---|
| 75 | 1002 |
| 1001 | 50 |
| 1003 | 20 |
| 1004 | 90 |
Combine tables whose columns drift with FULL UNION ALL BY NAME
A real-world case: an older export lacks coupon_code and a newer one lacks legacy_ref. Missing columns come back as NULL.
SELECT 'o1' AS order_id, 'R-77' AS legacy_ref
FULL UNION ALL BY NAME
SELECT 'o2' AS order_id, 'BLACKFRIDAY' AS coupon_code
ORDER BY order_id;| order_id | legacy_ref | coupon_code |
|---|---|---|
| o1 | R-77 | NULL |
| o2 | NULL | BLACKFRIDAY |
Common mistakes
- Writing a bare
UNION. BigQuery requiresUNION ALLorUNION DISTINCT; queries ported from PostgreSQL or MySQL, where UNION means DISTINCT, fail with "Expected keyword ALL or keyword DISTINCT". - Relying on column names to line up. Matching is positional, so a reordered SELECT list mixes values between columns without any error when the types are compatible. Use
UNION ALL BY NAMEor list columns in the same order. - Mixing operators without parentheses.
a UNION ALL b UNION DISTINCT cfails with "Different set operations cannot be used in the same query without using parentheses for grouping". - Unioning columns of incompatible types, such as an INT64 ID with a STRING ID. Cast both sides to the same type first.
- Using UNION DISTINCT to deduplicate arrays or JSON. Those types are not comparable; deduplicate on a key or on
TO_JSON_STRING(col)instead.
Cost and performance
- Prefer UNION ALL when duplicates are impossible or harmless: UNION DISTINCT adds a deduplication step over the whole combined result, which costs slots and memory on large inputs. On-demand bytes billed are the same, since both read the same columns.
- Each branch is billed for the columns it reads, so filter every branch on its partitioning column. A filter written only after the union may not be pushed into each branch; check the dry-run estimate.
FAQ
What is the difference between UNION ALL and UNION DISTINCT in BigQuery?
UNION ALL returns every row from both queries; UNION DISTINCT removes duplicate rows. BigQuery requires one of the two keywords; there is no bare UNION.
How do I union tables with different columns in BigQuery?
Use FULL UNION ALL BY NAME: columns are matched by name and those missing on one side are filled with NULL. With plain UNION ALL you have to add NULL AS missing_col to each query yourself.
Does BigQuery support MINUS or INTERSECT ALL?
No. Use EXCEPT DISTINCT instead of MINUS; INTERSECT and EXCEPT are only available as DISTINCT.
Related
Errors you may hit
More query syntax
All BigQuery query syntax · All statements and clauses · BigQuery error messages
