Request a demo

BigQuery · Array function

BigQuery ARRAY_SLICE

ARRAY_SLICE(arr, start_offset, end_offset) returns the elements between two zero-based offsets, both ends included. Negative offsets count from the end, so ARRAY_SLICE(arr, -3, -1) returns the last three elements.

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

Offsets are zero-based and inclusive at both ends: ARRAY_SLICE(arr, 0, 2) returns the first three elements. A negative offset counts back from the end, with -1 as the last element. Out-of-range offsets are clamped to the array instead of raising an error, so ARRAY_SLICE(arr, 1, 99) returns everything after the first element.

If the start position falls after the end position, the result is an empty array. If the array or either offset is NULL, the result is NULL. NULL elements inside the range are kept.

Ported code needs care with the end offset. Python, JavaScript and Snowflake's ARRAY_SLICE treat the end as exclusive; BigQuery includes it. PostgreSQL slices such as arr[2:4] are inclusive but one-based. A straight port is usually off by one.

Syntax

ARRAY_SLICE(array_to_slice, start_offset, end_offset)

Arguments and return value

ArgumentDescription
array_to_sliceThe input ARRAY.
start_offsetINT64, inclusive. 0 is the first element and -1 the last.
end_offsetINT64, inclusive, with the same rules. Values beyond either end are clamped.

Returns: An ARRAY with the same element type. Empty if the start comes after the end; NULL if any argument is NULL.

Examples

First three, last three, all but the first

SELECT
  ARRAY_SLICE(['view', 'cart', 'checkout', 'pay', 'confirm'], 0, 2) AS first_three,
  ARRAY_SLICE(['view', 'cart', 'checkout', 'pay', 'confirm'], -3, -1) AS last_three,
  ARRAY_SLICE(['view', 'cart', 'checkout', 'pay', 'confirm'], 1, -1) AS without_first;
Result
first_threelast_threewithout_first
[view, cart, checkout][checkout, pay, confirm][cart, checkout, pay, confirm]

Out-of-range and reversed offsets

SELECT
  ARRAY_SLICE([10, 20, 30], 1, 99) AS clamped_end,
  ARRAY_SLICE([10, 20, 30], -99, 0) AS clamped_start,
  ARRAY_SLICE([10, 20, 30], 2, 1) AS start_after_end,
  ARRAY_SLICE([10, 20, 30], 1, NULL) IS NULL AS null_offset;
Result
clamped_endclamped_startstart_after_endnull_offset
[20, 30][10][]true

Top 3 products per category

Slicing an ordered ARRAY_AGG works; putting LIMIT inside ARRAY_AGG returns the same arrays without building the full list first.

WITH sales AS (
  SELECT 'audio' AS category, 'headphones' AS product, 950 AS revenue UNION ALL
  SELECT 'audio', 'speaker', 1200 UNION ALL
  SELECT 'audio', 'cable', 80 UNION ALL
  SELECT 'audio', 'dac', 400 UNION ALL
  SELECT 'video', 'webcam', 700
)
SELECT
  category,
  ARRAY_SLICE(ARRAY_AGG(product ORDER BY revenue DESC), 0, 2) AS top_3,
  ARRAY_AGG(product ORDER BY revenue DESC LIMIT 3) AS top_3_limit
FROM sales
GROUP BY category
ORDER BY category;
Result
categorytop_3top_3_limit
audio[speaker, headphones, dac][speaker, headphones, dac]
video[webcam][webcam]

Common mistakes

  • Treating end_offset as exclusive, as in Python, JavaScript or Snowflake. ARRAY_SLICE(arr, 0, 3) returns four elements.
  • Using one-based positions from PostgreSQL. BigQuery offsets start at 0.
  • Expecting an error or NULL for out-of-range offsets. They are clamped, so a short array silently returns fewer elements.
  • Passing NULL to mean 'to the end'. A NULL offset makes the whole result NULL; use -1 for the last element.

Cost and performance

  • For top N per group, put the limit inside the aggregate, ARRAY_AGG(x ORDER BY y LIMIT n), rather than slicing an unlimited ARRAY_AGG. The unlimited version builds every group's full array first, which is slow and memory-hungry on large groups.

FAQ

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

ARRAY_SLICE(arr, -N, -1). For the last three, ARRAY_SLICE(arr, -3, -1); shorter arrays return all their elements.

Is the end offset in ARRAY_SLICE inclusive?

Yes. Both offsets are included, so ARRAY_SLICE(arr, 1, 3) returns three elements: offsets 1, 2 and 3.

How do I remove the first element of an array?

ARRAY_SLICE(arr, 1, -1) returns everything from the second element to the last. A one-element array becomes empty.

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.