BigQuery · Query syntax
BigQuery JOIN
JOIN combines rows of two tables on a condition: INNER JOIN keeps matches, LEFT, RIGHT and FULL OUTER JOIN also keep unmatched rows with NULLs, and CROSS JOIN pairs every row with every row. In BigQuery a comma or CROSS JOIN with UNNEST flattens an array column.
[INNER] JOIN ... ON condition returns the pairs of rows for which the condition is TRUE. LEFT [OUTER] JOIN also returns left rows without a match, with NULLs for the right columns; RIGHT JOIN does the same for right rows, and FULL [OUTER] JOIN for both sides. CROSS JOIN and the comma (FROM a, b) return every combination and take no condition. A NULL in a join key never matches anything, not even another NULL, so rows with NULL keys drop out of inner joins and come back unmatched from outer joins.
USING (col) is shorthand for equality on columns with the same name in both tables, and it returns the column once: SELECT * shows a single customer_id, and with a FULL JOIN that column is the COALESCE of both sides. With ON a.customer_id = b.customer_id both columns stay, and you must qualify them in the SELECT list, or the query fails with Column name is ambiguous.
Joins that reference an array of the left table are correlated: FROM orders, UNNEST(orders.items) AS item (or CROSS JOIN UNNEST) produces one row per array element and drops orders whose array is empty or NULL, while LEFT JOIN UNNEST(orders.items) AS item keeps them with a NULL item. This is how nested, repeated data is flattened in BigQuery; see UNNEST. RIGHT and FULL joins cannot be correlated.
BigQuery chooses between a broadcast join (the smaller side is sent to every worker) and a hash join (both sides are shuffled on the key). Join on columns of the same type: comparing INT64 with STRING fails with a No matching signature for operator = error, so cast one side. Joins on a non-unique key multiply rows; check key uniqueness before summing measures from the left table.
Syntax
FROM from_item
{ [INNER] JOIN | LEFT [OUTER] JOIN | RIGHT [OUTER] JOIN | FULL [OUTER] JOIN } from_item
{ ON bool_expression | USING (column[, ...]) }
FROM from_item CROSS JOIN from_item
FROM from_item, from_item
FROM table AS t, UNNEST(t.array_column) AS element
FROM table AS t LEFT JOIN UNNEST(t.array_column) AS elementArguments and return value
| Argument | Description |
|---|---|
ON bool_expression | Join condition. Rows are paired when it is TRUE; NULL counts as no match. |
USING (column, ...) | Equality on same-named columns, returned once in the output. Column names must not be qualified. |
CROSS JOIN / comma | Cartesian product: every row of one side with every row of the other. Also the correlated form for flattening arrays with UNNEST. |
Returns: A table of combined rows whose columns are those of both inputs (shared USING columns appear once).
Examples
FULL JOIN with USING keeps unmatched rows from both sides
Customer 3 has no orders and order 103 belongs to an unknown customer 9. An INNER JOIN would return only the first two rows. With USING, customer_id appears once, filled from whichever side has it.
WITH customers AS (
SELECT 1 AS customer_id, 'Acme' AS name UNION ALL
SELECT 2, 'Globex' UNION ALL
SELECT 3, 'Initech'
),
orders AS (
SELECT 101 AS order_id, 1 AS customer_id UNION ALL
SELECT 102, 2 UNION ALL
SELECT 103, 9
)
SELECT customer_id, c.name, o.order_id
FROM customers AS c
FULL JOIN orders AS o USING (customer_id)
ORDER BY customer_id;| customer_id | name | order_id |
|---|---|---|
| 1 | Acme | 101 |
| 2 | Globex | 102 |
| 3 | Initech | NULL |
| 9 | NULL | 103 |
Customers without orders with LEFT JOIN
WITH customers AS (
SELECT 1 AS customer_id, 'Acme' AS name UNION ALL
SELECT 2, 'Globex' UNION ALL
SELECT 3, 'Initech'
),
orders AS (
SELECT 101 AS order_id, 1 AS customer_id UNION ALL
SELECT 102, 2
)
SELECT c.customer_id, c.name
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;| customer_id | name |
|---|---|
| 3 | Initech |
Flatten an array with a comma join, and keep empty arrays with LEFT JOIN
Order 1003 has no items. The comma join drops it; LEFT JOIN UNNEST keeps it with a NULL SKU.
WITH orders AS (
SELECT 1001 AS order_id, ['SKU-1', 'SKU-2'] AS skus UNION ALL
SELECT 1003, ARRAY<STRING>[]
)
SELECT 'comma' AS join_type, o.order_id, sku
FROM orders AS o, UNNEST(o.skus) AS sku
UNION ALL
SELECT 'left', o.order_id, sku
FROM orders AS o LEFT JOIN UNNEST(o.skus) AS sku
ORDER BY join_type, order_id, sku;| join_type | order_id | sku |
|---|---|---|
| comma | 1001 | SKU-1 |
| comma | 1001 | SKU-2 |
| left | 1001 | SKU-1 |
| left | 1001 | SKU-2 |
| left | 1003 | NULL |
A filter in WHERE turns a LEFT JOIN into an inner join
The second query moves the condition on the right table into ON, so customers without a paid order survive with a NULL.
WITH customers AS (
SELECT 1 AS customer_id UNION ALL SELECT 2
),
orders AS (
SELECT 1 AS customer_id, 'paid' AS status UNION ALL
SELECT 2, 'refunded'
)
SELECT 'where' AS variant, c.customer_id, o.status
FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.status = 'paid'
UNION ALL
SELECT 'on', c.customer_id, o.status
FROM customers AS c LEFT JOIN orders AS o
ON o.customer_id = c.customer_id AND o.status = 'paid'
ORDER BY variant, customer_id;| variant | customer_id | status |
|---|---|---|
| on | 1 | paid |
| on | 2 | NULL |
| where | 1 | paid |
Common mistakes
- Filtering the right table of a LEFT JOIN in WHERE.
WHERE o.status = 'paid'removes the NULL rows the LEFT JOIN kept; put the condition in ON, as in the last example. - Joining on a key that is not unique and then summing. Each match repeats the left row, so
SUM(order_total)after joining order lines counts every order once per line. Aggregate before joining or deduplicate the key. - Expecting NULL keys to match.
NULL = NULLis not TRUE; useON a.k = b.k OR (a.k IS NULL AND b.k IS NULL)or replace NULLs with a sentinel if they must pair up. - Flattening arrays with a comma join and losing rows. Parents with empty or NULL arrays disappear; use
LEFT JOIN UNNEST(...)to keep them. - Joining INT64 to STRING keys. BigQuery does not coerce them; cast one side with
CAST(id AS STRING)orSAFE_CAST.
Cost and performance
- On-demand, a join is billed for the columns read from each table, not for the rows produced. Filter both sides on their partitioning columns: a filter on the fact table does not prune the partitions of the other table.
- Joins on a key with a few very frequent values (for example NULL or 'unknown') send all those rows to one worker and can fail with Resources exceeded. Filter out or split hot keys before joining.
- An accidental cross join, for example a forgotten ON in a comma join between two tables, returns M times N rows and can run for a long time; it does not change the bytes billed, but it burns slots.
FAQ
What is the difference between USING and ON in a BigQuery join?
USING (id) joins on equality of same-named columns and returns id once; ON a.id = b.id keeps both columns, which then have to be qualified. ON also allows any condition, such as ranges or several columns with different names.
What does a comma mean in the FROM clause in BigQuery?
It is a CROSS JOIN. With UNNEST(t.array_col) on the right it is correlated, producing one row per array element, which is the usual way to flatten repeated fields.
How do I find rows in one table that are not in another?
Use LEFT JOIN ... WHERE right.key IS NULL, WHERE NOT EXISTS (SELECT 1 FROM b WHERE b.key = a.key), or EXCEPT DISTINCT on the key columns. Avoid NOT IN (subquery) when the subquery can return NULL: then it returns no rows.
Related
Errors you may hit
More query syntax
All BigQuery query syntax · All statements and clauses · BigQuery error messages
