Request a demo

BigQuery · Array function

BigQuery ARRAY_LENGTH

ARRAY_LENGTH(arr) returns the number of elements in an array as INT64: 0 for an empty array and NULL for a NULL array. Use it to count items per row, find rows with empty arrays, or compute the offset of the last element.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

ARRAY_LENGTH counts every element, duplicates and NULL elements included. It takes exactly one argument: BigQuery arrays are one-dimensional, so there is no dimension parameter as in PostgreSQL's array_length(arr, 1). Snowflake's ARRAY_SIZE and PostgreSQL's cardinality() map to it; neither name exists in BigQuery.

NULL and empty are different values inside a query. ARRAY_LENGTH of a NULL array is NULL, so WHERE ARRAY_LENGTH(tags) = 0 misses NULL arrays. Wrap it in IFNULL(ARRAY_LENGTH(tags), 0) when both should count as empty. Arrays stored in a table are never NULL, because BigQuery writes a NULL array as an empty one, but computed arrays (from a LEFT JOIN, a JSON function on a missing key, a CASE) can be.

For strings, use LENGTH. For a JSON array, convert it first with JSON_QUERY_ARRAY. Because offsets are zero-based, ARRAY_LENGTH(arr) - 1 is the offset of the last element, though ARRAY_LAST reads better.

Syntax

ARRAY_LENGTH(array_expression)

Arguments and return value

ArgumentDescription
array_expressionAny ARRAY value.

Returns: INT64: 0 for an empty array, NULL for a NULL array.

Examples

Items per order, including empty and NULL arrays

WITH orders AS (
  SELECT 'A-100' AS order_id, ['keyboard', 'mouse', 'cable'] AS items UNION ALL
  SELECT 'A-101', ['monitor'] UNION ALL
  SELECT 'A-102', ARRAY<STRING>[] UNION ALL
  SELECT 'A-103', CAST(NULL AS ARRAY<STRING>)
)
SELECT
  order_id,
  ARRAY_LENGTH(items) AS item_count,
  IFNULL(ARRAY_LENGTH(items), 0) AS item_count_or_zero
FROM orders
ORDER BY order_id;
Result
order_iditem_countitem_count_or_zero
A-10033
A-10111
A-10200
A-103NULL0

NULL elements and duplicates are counted

To count distinct values, unnest the array and use COUNT(DISTINCT).

SELECT
  ARRAY_LENGTH(['a', NULL, 'c']) AS with_null_element,
  (SELECT COUNT(DISTINCT x) FROM UNNEST(['a', 'a', 'c']) AS x) AS distinct_elements;
Result
with_null_elementdistinct_elements
32

Length of a JSON array

SELECT ARRAY_LENGTH(
  JSON_QUERY_ARRAY(JSON '{"items": [{"sku": "KB-1"}, {"sku": "MS-2"}]}', '$.items')
) AS item_count;
Result
item_count
2

Sessions by number of pages viewed

WITH sessions AS (
  SELECT 's1' AS session_id, ['/home', '/pricing', '/signup'] AS pages UNION ALL
  SELECT 's2', ['/blog'] UNION ALL
  SELECT 's3', ['/docs'] UNION ALL
  SELECT 's4', ['/home', '/docs']
)
SELECT ARRAY_LENGTH(pages) AS pages_viewed, COUNT(*) AS sessions
FROM sessions
GROUP BY pages_viewed
ORDER BY pages_viewed;
Result
pages_viewedsessions
12
21
31

Common mistakes

  • Calling LENGTH on an array. LENGTH accepts only STRING and BYTES and fails with No matching signature for function LENGTH.
  • Testing ARRAY_LENGTH(arr) = 0 on computed arrays that can be NULL. Use IFNULL(ARRAY_LENGTH(arr), 0) = 0 so NULL arrays count as empty.
  • Porting array_length(arr, 1) from PostgreSQL or ARRAY_SIZE(arr) from Snowflake. BigQuery's function takes one argument, and ARRAY_SIZE and CARDINALITY don't exist.
  • Counting distinct values with ARRAY_LENGTH. Duplicates are counted; use (SELECT COUNT(DISTINCT x) FROM UNNEST(arr) AS x).

Cost and performance

  • On a repeated record such as GA4's event_params, ARRAY_LENGTH(event_params) references the whole record, so on-demand billing counts every field inside it. (SELECT COUNT(key) FROM UNNEST(event_params)) returns the same number when key is never NULL and reads only the key field, which a dry run confirms.

FAQ

How do I count the elements in an array in BigQuery?

Use ARRAY_LENGTH(arr). It returns an INT64 and counts duplicates and NULL elements.

Why does ARRAY_LENGTH return NULL instead of 0?

The array itself is NULL, for example a missing JSON key or an unmatched row of a LEFT JOIN. Use IFNULL(ARRAY_LENGTH(arr), 0) when you want 0.

How do I find rows with an empty array?

For a stored array column, WHERE ARRAY_LENGTH(arr) = 0, because stored arrays are never NULL. For computed arrays, include NULL: WHERE IFNULL(ARRAY_LENGTH(arr), 0) = 0.

Errors you may hit

More array functions

All BigQuery array functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.