BigQuery · Array function
BigQuery ARRAY
ARRAY(subquery) collects the rows a subquery returns into an array, one element per row, and returns an empty array (never NULL) when there are no rows. Use it to build array columns, or to filter and reshape an existing array with ARRAY(SELECT ... FROM UNNEST(arr)).
ARRAY takes a subquery and turns its result into an ARRAY value. The subquery must return exactly one column, and each row becomes one element. To collect several columns, write SELECT AS STRUCT col1, col2 and you get an array of structs. Elements follow the subquery's ORDER BY; without one, their order is arbitrary.
The most useful pattern is a correlated subquery over UNNEST: ARRAY(SELECT x FROM UNNEST(arr) AS x WHERE ... ORDER BY ...) filters, transforms, deduplicates (SELECT DISTINCT) or sorts an array inside each row, with no GROUP BY. The subquery can also read another table, keyed by the outer row, to attach a list of related rows such as order lines.
Array literals are the other way to build arrays: [1, 2, 3], ['paid', 'shipped'], or typed ARRAY<FLOAT64>[1, 2.5]. All elements must share a type. A bare [] takes its type from context and defaults to ARRAY<INT64>; write ARRAY<STRING>[] to be explicit.
Two limits apply to every array in BigQuery. An array can't contain another array, so wrap the inner one in a struct. And an array that reaches the query result or a table can't contain NULL elements; that fails with Array cannot have a null element. For one array per group of rows, use the aggregate ARRAY_AGG instead.
Syntax
ARRAY(subquery)
[expression[, ...]]
ARRAY<element_type>[expression[, ...]]Arguments and return value
| Argument | Description |
|---|---|
subquery | A subquery that returns one column, or several columns wrapped with SELECT AS STRUCT. It can reference columns of the outer query. |
expression | In an array literal, the elements. They must share a common supertype. |
Returns: ARRAY<T>, where T is the type of the subquery's column (a STRUCT for SELECT AS STRUCT). An empty array when the subquery returns no rows.
Examples
Array literals, typed and empty
SELECT
['paid', 'shipped'] AS statuses,
ARRAY<FLOAT64>[1, 2.5] AS amounts,
ARRAY<STRING>[] AS no_tags;| statuses | amounts | no_tags |
|---|---|---|
| [paid, shipped] | [1.0, 2.5] | [] |
Attach order lines to each order with SELECT AS STRUCT
A correlated subquery reads the lines of each order. Two columns per element need SELECT AS STRUCT.
WITH orders AS (
SELECT 'A-100' AS order_id UNION ALL
SELECT 'A-101'
),
order_lines AS (
SELECT 'A-100' AS order_id, 'keyboard' AS sku, 1 AS qty UNION ALL
SELECT 'A-100', 'cable', 3 UNION ALL
SELECT 'A-101', 'monitor', 2
)
SELECT
o.order_id,
ARRAY(
SELECT AS STRUCT l.sku, l.qty
FROM order_lines AS l
WHERE l.order_id = o.order_id
ORDER BY l.sku
) AS lines
FROM orders AS o
ORDER BY o.order_id;| order_id | lines |
|---|---|
| A-100 | [{cable, 3}, {keyboard, 1}] |
| A-101 | [{monitor, 2}] |
Filter and transform an array in place
Drop zero-amount lines, convert cents to dollars, and keep the original line order with WITH OFFSET.
WITH carts AS (
SELECT 'c-1' AS cart_id, [1999, 0, 450, 0, 1250] AS line_cents
)
SELECT
cart_id,
ARRAY(
SELECT cents / 100
FROM UNNEST(line_cents) AS cents WITH OFFSET AS line
WHERE cents > 0
ORDER BY line
) AS paid_lines_usd
FROM carts;| cart_id | paid_lines_usd |
|---|---|
| c-1 | [19.99, 4.5, 12.5] |
No rows: an empty array, not NULL
ARRAY returns [] when the subquery finds nothing. ARRAY_AGG over zero rows returns NULL, which matters in IS NULL checks and COALESCE.
SELECT
ARRAY(SELECT x FROM UNNEST([4, 8, 15]) AS x WHERE x > 100) AS from_array_fn,
(SELECT ARRAY_AGG(x) FROM UNNEST([4, 8, 15]) AS x WHERE x > 100) IS NULL AS array_agg_is_null;| from_array_fn | array_agg_is_null |
|---|---|
| [] | true |
Common mistakes
- Returning two columns from the subquery.
ARRAY(SELECT sku, qty FROM ...)fails with "ARRAY subquery cannot have more than one column unless using SELECT AS STRUCT"; writeSELECT AS STRUCT sku, qty. - Relying on element order without ORDER BY inside the subquery. The order is not guaranteed and can change between runs.
- Producing NULL elements.
ARRAY(SELECT discount FROM ...)works mid-query but fails when the array reaches the result if any discount is NULL. Filter withWHERE discount IS NOT NULL. - Building an array of arrays. A subquery that returns an ARRAY column fails because nested arrays aren't supported;
SELECT AS STRUCT tagsreturns an array of structs that each hold an array. - Expecting NULL when nothing matches. ARRAY returns
[], soIS NULLchecks never fire; testARRAY_LENGTH(arr) = 0instead.
Cost and performance
- An ARRAY subquery over
UNNEST(col)works inside each row: it neither multiplies rows nor needs a GROUP BY. Flattening with a join and rebuilding the array withARRAY_AGG ... GROUP BYgives the same result with an extra shuffle.
FAQ
What is the difference between ARRAY and ARRAY_AGG in BigQuery?
ARRAY turns the rows of a subquery into one array and returns [] when there are no rows. ARRAY_AGG is an aggregate that builds one array per GROUP BY group and returns NULL for zero rows.
How do I remove duplicates from an array in BigQuery?
ARRAY(SELECT DISTINCT x FROM UNNEST(arr) AS x ORDER BY x). Without the ORDER BY, the order of the distinct values is arbitrary.
How do I create an empty array of a specific type?
Write a typed literal such as ARRAY<STRING>[]. A bare [] takes its type from the surrounding expression and falls back to ARRAY<INT64>.
Related
Errors you may hit
More array functions
All BigQuery array functions · All functions · BigQuery error messages
