Christopher Alphonse
HomeBlogProjectsAboutResumeLLM Guide
Content
BlogRSS
Profile
AboutResumeProjects
Site
HomeSitemap

© 2022–2026 Christopher Alphonse

  1. Home
  2. Blog
  3. Groq-by-sanity-cms-a-clear-and-practical-guide

Christopher Alphonse 's image

Christopher Alphonse

/Christopheralphonse
4 min(s) read
6
views
6/05/26

GROQ Queries: Complete Guide for Sanity CMS

Master GROQ queries in 10 minutes. Learn how to filter, sort, join, and paginate content from Sanity CMS with real examples. Perfect for Sanity developers of all levels.

sanity-cms-image

GROQ (Graph-Relational Object Queries) is a query language for JSON documents, designed and maintained by Sanity CMS. It lets you filter, sort, join, and project structured content using a compact, readable syntax — no SQL or GraphQL required.

Last updated: June 2026

What Is GROQ?¶

GROQ is to Sanity what SQL is to relational databases and GraphQL is to API-driven applications. It is a native query language for querying, filtering, joining, and transforming JSON documents stored in a Sanity content lake.

Unlike SQL, GROQ handles deeply nested JSON naturally. Unlike GraphQL, GROQ does not require a schema to query — you write filters and projections directly.

Basic GROQ Query Syntax¶

Filter by Document Type¶

Every GROQ query typically starts by filtering documents of a specific type.

groq
*[_type == "post"]

This returns all documents where _type equals "post".

Add Field Conditions¶

Chain additional conditions with logical operators.

groq
*[_type == "post" && published == true && category == "tech"]

Project Specific Fields¶

Use curly braces to control which fields the query returns.

groq
*[_type == "post"]{title, slug, publishedAt}

Sorting and Ordering¶

Sort results with the order() function. Default order is by _createdAt.

groq
*[_type == "post"] | order(publishedAt desc) *[_type == "post"] | order(title asc) *[_type == "post"] | order(publishedAt desc, title asc)

Pagination¶

GROQ uses slice syntax for pagination — no cursors required.

groq
// First 20 posts *[_type == "post"] | order(_createdAt desc) [0...20] // Next 20 posts (page 2) *[_type == "post"] | order(_createdAt desc) [20...40]

Reference Resolution¶

The -> operator resolves document references inline — GROQ's equivalent of a JOIN.

groq
*[_type == "post"]{ title, "authorName": author->name, "categoryTitle": category->title }

This resolves the author reference field to the referenced document's name field in a single query — no separate API calls needed.

Advanced GROQ Patterns¶

Count Documents¶

groq
count(*[_type == "post" && published == true])

Select with Conditional Logic¶

groq
*[_type == "post"]{ title, "status": select( published == true => "published", _createdAt > now() - 7 => "recent", "draft" ) }

Global Object References¶

groq
// Get site-wide settings *[_id == "global-config"][0]

Filter by Reference¶

groq
*[_type == "post" && author->_id == $authorId]

Build Better Apps With Reinvoice¶

Running a SaaS? Reinvoice handles invoicing, subscription management, and billing automations so you can focus on code. Start your free trial .

Frequently Asked Questions¶

Is GROQ faster than GraphQL?

GROQ queries run natively in Sanity's content lake without a separate API server. For Sanity projects, GROQ is typically faster because it avoids the GraphQL middleware layer.

Can GROQ do joins?

Yes. The -> operator resolves document references inline, similar to a SQL JOIN. It can traverse multiple levels: author->company->name.

How do I paginate GROQ queries?

Use slice syntax [start...end]. Page 1: [0...20], Page 2: [20...40]. Combine with | order() for consistent ordering across pages.

What is the difference between GROQ and GraphQL?

GROQ is a query language for JSON documents designed by Sanity. GraphQL is a query language for APIs. GROQ runs directly on the content lake; GraphQL requires a resolver layer.

Can I use GROQ with other databases?

GROQ is designed for Sanity's content lake. Other databases have their own query languages (SQL, MongoDB aggregation pipeline, etc.) that serve similar purposes with different syntax.

Table of Contents

  • What Is GROQ?
  • Basic GROQ Query Syntax
  • Filter by Document Type
  • Add Field Conditions
  • Project Specific Fields
  • Sorting and Ordering
  • Pagination
  • Reference Resolution
  • Advanced GROQ Patterns
  • Count Documents
  • Select with Conditional Logic
  • Global Object References
  • Filter by Reference
  • Build Better Apps With Reinvoice
  • Frequently Asked Questions

Tags

    Sanity CMS
    GROQ

Share Post

Sponsored

Try Reinvoice

Send professional invoices in seconds.

Start free trial →

Featured

Systems

Front-End Complexity: Why Simple UI Tasks Take 10x Longer

Why simple front-end tasks take 10x longer. Every UI element is a distributed sy...

machine hallucinations

Why Claude's Hallucinations Made Me a Better Developer

AI hallucinations feel like bugs, but they are actually a forcing function for b...