Request a demo

BigQuery · Array function

BigQuery ARRAY_FIRST

ARRAY_FIRST(arr) returns the first element of an array, typed as the array's elements. It returns NULL for a NULL array but raises an error for an empty one, so use arr[SAFE_OFFSET(0)] when arrays can be empty.

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

ARRAY_FIRST is a readable shortcut for arr[OFFSET(0)], and like it, it fails on an empty array: ARRAY_FIRST with "ARRAY_FIRST cannot get the first element of an empty array", the subscript with Array index 0 is out of bounds. The SAFE. prefix isn't supported for this function, so the empty-safe form is arr[SAFE_OFFSET(0)], which returns NULL.

A common use is the first value per group: ARRAY_FIRST(ARRAY_AGG(channel ORDER BY touched_at)) returns each user's first-touch channel. MIN_BY does the same in one call, and FIRST_VALUE does it as a window function when you need it on every row.

Coming from PostgreSQL, remember that BigQuery subscripts are zero-based by default: arr[1] is the second element. Use arr[ORDINAL(1)] when you want one-based positions.

Syntax

ARRAY_FIRST(array_expression)

Arguments and return value

ArgumentDescription
array_expressionA non-empty ARRAY of any element type.

Returns: The array's element type. NULL if the array is NULL.

Examples

First element, and a NULL array

SELECT
  ARRAY_FIRST(['/home', '/pricing', '/signup']) AS landing_page,
  ARRAY_FIRST(CAST(NULL AS ARRAY<STRING>)) AS from_null_array;
Result
landing_pagefrom_null_array
/homeNULL

Arrays that can be empty: use SAFE_OFFSET(0)

ARRAY_FIRST(pages) would fail on session s2. pages[SAFE_OFFSET(0)] returns NULL instead.

WITH sessions AS (
  SELECT 's1' AS session_id, ['/home', '/pricing'] AS pages UNION ALL
  SELECT 's2', ARRAY<STRING>[]
)
SELECT
  session_id,
  pages[SAFE_OFFSET(0)] AS landing_page
FROM sessions
ORDER BY session_id;
Result
session_idlanding_page
s1/home
s2NULL

First-touch channel per user

WITH touches AS (
  SELECT 'u1' AS user_id, TIMESTAMP '2026-11-01 09:00:00' AS touched_at, 'organic' AS channel UNION ALL
  SELECT 'u1', TIMESTAMP '2026-11-03 18:30:00', 'email' UNION ALL
  SELECT 'u2', TIMESTAMP '2026-11-02 12:00:00', 'paid_search'
)
SELECT
  user_id,
  ARRAY_FIRST(ARRAY_AGG(channel ORDER BY touched_at)) AS first_touch
FROM touches
GROUP BY user_id
ORDER BY user_id;
Result
user_idfirst_touch
u1organic
u2paid_search

Common mistakes

  • Calling ARRAY_FIRST where some arrays are empty. One empty array fails the whole query; use arr[SAFE_OFFSET(0)].
  • Writing SAFE.ARRAY_FIRST(arr). BigQuery rejects it with "SAFE with function array_first is not supported".
  • Taking the first element of an ARRAY_AGG without ORDER BY. The result is arbitrary; sort inside the aggregate.
  • Using arr[1] for the first element, as in PostgreSQL. In BigQuery that is the second element.

FAQ

How do I get the first element of an array in BigQuery?

ARRAY_FIRST(arr) or arr[OFFSET(0)]. If the array can be empty, use arr[SAFE_OFFSET(0)], which returns NULL instead of failing.

What is the difference between ARRAY_FIRST and arr[SAFE_OFFSET(0)]?

They return the same value for a non-empty array. On an empty array, ARRAY_FIRST raises an error and SAFE_OFFSET(0) returns NULL.

How do I get the first value per group?

ARRAY_FIRST(ARRAY_AGG(value ORDER BY ts)), or MIN_BY(value, ts), which returns the value from the row with the smallest ts.

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.