Request a demo

BigQuery · Array function

BigQuery ARRAY_LAST

ARRAY_LAST(arr) returns the last element of an array without the ARRAY_LENGTH(arr) - 1 arithmetic. It returns NULL for a NULL array and raises an error for an empty one. Pair it with SPLIT to get the last part of a path or URL.

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

ARRAY_LAST reads the final element of an array and returns it with the element type. It replaces the older idiom arr[OFFSET(ARRAY_LENGTH(arr) - 1)]. An empty array raises "ARRAY_LAST cannot get the last element of an empty array", and the SAFE. prefix isn't supported, so for arrays that can be empty use arr[SAFE_OFFSET(ARRAY_LENGTH(arr) - 1)], which returns NULL.

BigQuery has no negative subscripts: arr[OFFSET(-1)] fails with Array index -1 is out of bounds. ARRAY_LAST, or ARRAY_SLICE with negative offsets, are the ways to read from the end.

Two patterns cover most uses: ARRAY_LAST(SPLIT(path, '/')) for the last segment of a path, file name or URL, and ARRAY_LAST(ARRAY_AGG(status ORDER BY changed_at)) for the latest value per group. MAX_BY is a one-call alternative for the second.

Syntax

ARRAY_LAST(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

Exit page and file name

SELECT
  ARRAY_LAST(['/home', '/pricing', '/signup']) AS exit_page,
  ARRAY_LAST(SPLIT('gs://exports/2026/11/orders.csv', '/')) AS file_name;
Result
exit_pagefile_name
/signuporders.csv

Arrays that can be empty

ARRAY_LAST(pages) would fail on s2. For an empty array the computed offset is -1, which SAFE_OFFSET treats as out of range and turns into NULL.

WITH sessions AS (
  SELECT 's1' AS session_id, ['/home', '/pricing', '/signup'] AS pages UNION ALL
  SELECT 's2', ARRAY<STRING>[]
)
SELECT
  session_id,
  pages[SAFE_OFFSET(ARRAY_LENGTH(pages) - 1)] AS exit_page
FROM sessions
ORDER BY session_id;
Result
session_idexit_page
s1/signup
s2NULL

Current status per order

WITH status_changes AS (
  SELECT 'A-100' AS order_id, TIMESTAMP '2026-11-20 08:00:00' AS changed_at, 'created' AS status UNION ALL
  SELECT 'A-100', TIMESTAMP '2026-11-20 08:05:00', 'paid' UNION ALL
  SELECT 'A-100', TIMESTAMP '2026-11-21 14:00:00', 'shipped' UNION ALL
  SELECT 'A-101', TIMESTAMP '2026-11-22 10:00:00', 'created'
)
SELECT
  order_id,
  ARRAY_LAST(ARRAY_AGG(status ORDER BY changed_at)) AS current_status
FROM status_changes
GROUP BY order_id
ORDER BY order_id;
Result
order_idcurrent_status
A-100shipped
A-101created

Common mistakes

  • Calling ARRAY_LAST where some arrays are empty. One empty array fails the query; use arr[SAFE_OFFSET(ARRAY_LENGTH(arr) - 1)].
  • Writing arr[OFFSET(-1)] or arr[-1] as in Python. Negative positions are out of bounds in BigQuery.
  • Reading the last element of an ARRAY_AGG built without ORDER BY. The result can change between runs.
  • Splitting text that ends with the delimiter. ARRAY_LAST(SPLIT('a/b/', '/')) returns an empty string, not b; trim the trailing delimiter first with RTRIM(path, '/').

FAQ

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

ARRAY_LAST(arr). For arrays that can be empty, use arr[SAFE_OFFSET(ARRAY_LENGTH(arr) - 1)], which returns NULL instead of failing.

How do I get the text after the last slash or delimiter?

ARRAY_LAST(SPLIT(path, '/')). For example, ARRAY_LAST(SPLIT('gs://exports/2026/11/orders.csv', '/')) returns orders.csv.

Does BigQuery support negative array indexes?

No. arr[OFFSET(-1)] raises an out-of-bounds error and arr[SAFE_OFFSET(-1)] returns NULL. Use ARRAY_LAST or ARRAY_SLICE(arr, -n, -1).

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.