BigQuery · Aggregate function
BigQuery ARRAY_AGG
ARRAY_AGG collects the values of a group into an ARRAY. With ORDER BY and LIMIT inside the call it keeps the latest row per key: ARRAY_AGG(t ORDER BY updated_at DESC LIMIT 1)[OFFSET(0)]. Add IGNORE NULLS, because an array with a NULL element fails the query.
ARRAY_AGG returns one ARRAY per group. Inside the call you can add IGNORE NULLS or RESPECT NULLS, DISTINCT, ORDER BY and LIMIT, which BigQuery applies in that order. Without ORDER BY the element order is arbitrary. The argument can be any type except ARRAY, including a STRUCT or a whole row: ARRAY_AGG(t) where t is the table alias.
NULLs need care. ARRAY_AGG keeps NULL values by default, but BigQuery can't return an array that contains NULL, so SELECT ARRAY_AGG(x) over a column with NULLs fails with Array cannot have a null element; error in writing field. Add IGNORE NULLS. An array that stays inside the query, as in ARRAY_LENGTH(ARRAY_AGG(x)), may hold NULLs. With no input rows ARRAY_AGG returns NULL, which result tables show as an empty array [].
The most common pattern keeps one row per key. ARRAY_AGG(t ORDER BY updated_at DESC LIMIT 1)[OFFSET(0)] returns the newest row of each group as a STRUCT, and SELECT latest.* expands it back into columns. It answers the same question as QUALIFY ROW_NUMBER() OVER (PARTITION BY ... ORDER BY updated_at DESC) = 1 with a GROUP BY instead of a window.
Syntax
ARRAY_AGG([DISTINCT] expression [{IGNORE | RESPECT} NULLS] [WHERE where_expression] [ORDER BY key [{ASC | DESC}] [, ...]] [LIMIT n]) [OVER over_clause]Arguments and return value
| Argument | Description |
|---|---|
expression | Any type except ARRAY. A STRUCT or a table alias collects whole rows. To merge arrays into one, use ARRAY_CONCAT_AGG. |
IGNORE NULLS | RESPECT NULLS | IGNORE NULLS drops NULL values. RESPECT NULLS, the default, keeps them, which fails if the array ends up in the query result. |
DISTINCT | Keeps each distinct value once. With ORDER BY, the sort key must be expression itself. |
ORDER BY key | Orders the elements. Without it the order is arbitrary. |
LIMIT n | Keeps the first n elements after ordering. n must be a constant INT64. |
WHERE where_expression | Optional filter inside the call (Preview). It requires IGNORE NULLS. |
over_clause | Makes ARRAY_AGG a window function. ORDER BY and LIMIT inside the call are then rejected. |
Returns: ARRAY of the expression's type. NULL when there are no input rows.
Examples
Items of each order, in line order
WITH order_lines AS (
SELECT 1001 AS order_id, 2 AS line_no, 'mug' AS sku UNION ALL
SELECT 1001, 1, 'beans-1kg' UNION ALL
SELECT 1002, 1, 'grinder'
)
SELECT
order_id,
ARRAY_AGG(sku ORDER BY line_no) AS skus
FROM order_lines
GROUP BY order_id
ORDER BY order_id;| order_id | skus |
|---|---|
| 1001 | [beans-1kg, mug] |
| 1002 | [grinder] |
IGNORE NULLS
Without IGNORE NULLS this query fails with Array cannot have a null element; error in writing field sources.
WITH visits AS (
SELECT 's1' AS session_id, 'google' AS utm_source UNION ALL
SELECT 's1', NULL UNION ALL
SELECT 's1', 'newsletter'
)
SELECT
session_id,
ARRAY_AGG(utm_source IGNORE NULLS ORDER BY utm_source) AS sources
FROM visits
GROUP BY session_id;| session_id | sources |
|---|---|
| s1 | [google, newsletter] |
Latest row per key
v is the whole row as a STRUCT. ORDER BY ... DESC LIMIT 1 keeps the newest version of each customer, and latest.* turns the STRUCT back into columns.
WITH customer_versions AS (
SELECT 'c1' AS customer_id, 'free' AS plan, TIMESTAMP '2026-10-01 09:00:00 UTC' AS updated_at UNION ALL
SELECT 'c1', 'pro', TIMESTAMP '2026-11-15 14:30:00 UTC' UNION ALL
SELECT 'c2', 'pro', TIMESTAMP '2026-09-20 08:00:00 UTC' UNION ALL
SELECT 'c2', 'enterprise', TIMESTAMP '2026-11-02 11:00:00 UTC'
)
SELECT latest.*
FROM (
SELECT ARRAY_AGG(v ORDER BY updated_at DESC LIMIT 1)[OFFSET(0)] AS latest
FROM customer_versions AS v
GROUP BY v.customer_id
)
ORDER BY customer_id;| customer_id | plan | updated_at |
|---|---|---|
| c1 | pro | 2026-11-15 14:30:00 UTC |
| c2 | enterprise | 2026-11-02 11:00:00 UTC |
Last three pages of a session
WITH page_views AS (
SELECT 's1' AS session_id, '/home' AS page, 1 AS seq UNION ALL
SELECT 's1', '/pricing', 2 UNION ALL
SELECT 's1', '/docs', 3 UNION ALL
SELECT 's1', '/signup', 4
)
SELECT
session_id,
ARRAY_AGG(page ORDER BY seq DESC LIMIT 3) AS last_three_pages
FROM page_views
GROUP BY session_id;| session_id | last_three_pages |
|---|---|
| s1 | [/signup, /docs, /pricing] |
Common mistakes
- Returning arrays that contain NULL. The query fails with Array cannot have a null element. Add IGNORE NULLS, or replace NULLs with IFNULL before aggregating. PostgreSQL keeps NULL elements and Snowflake drops them silently, so ported queries behave differently.
- Expecting the query's ORDER BY to order the elements. Put ORDER BY inside the call; otherwise the order is arbitrary and can change between runs.
- Aggregating arrays.
ARRAY_AGG(tags)wheretagsis an ARRAY fails withThe argument to ARRAY_AGG must not be an array type. Use ARRAY_CONCAT_AGG to merge arrays, or wrap each array in a STRUCT to build an array of structs. - Combining DISTINCT with ORDER BY on another column.
ARRAY_AGG(DISTINCT sku ORDER BY line_no)is rejected; the sort key must be the aggregated value. - Collecting unbounded groups. One array per user holding every event can exceed memory or the 100 MB row limit. Cap it with LIMIT inside the call.
Cost and performance
- Each group's array is built in memory. A heavy key (a bot user, a NULL or default ID) can fail the query with Resources exceeded.
ARRAY_AGG(x ORDER BY ts DESC LIMIT 10)keeps only what you need.
FAQ
How do I get the latest row per ID in BigQuery?
Group by the ID and take ARRAY_AGG(t ORDER BY updated_at DESC LIMIT 1)[OFFSET(0)], then SELECT latest.*. The window alternative is QUALIFY ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) = 1; see ROW_NUMBER.
Why do I get 'Array cannot have a null element'?
An array in the query result contains a NULL. Add IGNORE NULLS inside ARRAY_AGG, or replace NULLs with a default value before aggregating.
How do I turn an array back into rows?
Use UNNEST: SELECT order_id, sku FROM orders, UNNEST(skus) AS sku. WITH OFFSET adds each element's position.
What is the difference between ARRAY_AGG and STRING_AGG?
ARRAY_AGG keeps the values and their type, so you can index, unnest or aggregate STRUCTs. STRING_AGG joins strings into one text value. Use ARRAY_AGG for further processing and STRING_AGG for display.
Related
Errors you may hit
More aggregate functions
All BigQuery aggregate functions · All functions · BigQuery error messages
