The Sanity engineering blog is live. Deep dives from our engineers.

Query last 10 elements from array in Sanity GROQ

7 repliesLast updated: Nov 29, 2025

Hi,I'm trying to query an array but I want the last 10 elements of that array. Is it possible to do this one one query? This is what I'm trying to do and I get an error:

  *[_type == "archives"] {
    title,
    slug,
    links[(count(links) - 10))...count(links))]
  }
In this case
links
is an array.
AI Update

GROQ doesn't support negative indices in array slicing the way you might expect from other languages. Your syntax links[(count(links) - 10))...count(links))] has a couple of issues.

To get the last 10 elements of an array in GROQ, you have a few options:

GROQ does support negative indices! According to the GROQ pipeline components documentation, negative range endpoints are based at the end of the array. You can use:

*[_type == "archives"] {
  title,
  slug,
  "lastTenLinks": links[-10..-1]
}

This syntax [-10..-1] means "from the 10th-to-last element to the last element."

Option 2: Order and slice from the beginning

If you want to be more explicit or if negative indexing doesn't work in your GROQ version, you can reverse the array and take the first 10:

*[_type == "archives"] {
  title,
  slug,
  "lastTenLinks": links | order(@) | [0...10]
}

However, this approach may not preserve the original order.

Why your original query failed

Your query links[(count(links) - 10))...count(links))] has these problems:

The solution

Use the negative index approach with two dots (which includes both endpoints):

*[_type == "archives"] {
  title,
  slug,
  links[-10..-1]
}

This will gracefully handle arrays shorter than 10 elements - it'll just return whatever is available. The negative indices are based at the end of the array, so [-10..-1] gives you the last 10 items (or fewer if the array is shorter).

Show original thread
7 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions