# Functions, in my queries?

https://www.sanity.io/learn/course/between-groq-and-a-hard-place/functions-in-my-queries

With GROQ you can do more than return values; you can compute and transform them from within the query.

---

## Navigation

**Track:** [Mastering content operations](https://www.sanity.io/learn/track/sanity-developer-essentials) · [View as markdown](https://www.sanity.io/learn/track/sanity-developer-essentials.md)

**Course:** [Between GROQ and a hard place](https://www.sanity.io/learn/course/between-groq-and-a-hard-place) · [View as markdown](https://www.sanity.io/learn/course/between-groq-and-a-hard-place.md)

**Previous:** [Filters and projections](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/filters-and-projections) · **Next:** [Custom functions](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/custom-functions)

---

## Course Contents

1. [Your new favorite query language](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/your-new-favourite-query-language)
2. [The Vision Tool](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/the-vision-tool)
3. [Filters and projections](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/filters-and-projections)
4. **Functions, in my queries?** *(current)*
5. [Custom functions](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/custom-functions)
6. [Joins and subqueries](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/joins-and-subqueries)
7. [Chained projections](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/chained-projections)
8. [Query parameters](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/conditional-parameters)
9. [Handling missing values](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/null-handling-and-prevention)

---

> [Video: Functions, in my queries?](https://www.sanity.io/learn/course/between-groq-and-a-hard-place/functions-in-my-queries)

GROQ offers a [number of Functions](https://www.sanity.io/docs/groq-functions) that you can experiment with. These will save you from needing to "post-process" data in your front end or API to shape data as desired from within the query.



Here are a few simple examples:



- [ ] `count()` all the `event` documents in the dataset


```json
count(*[_type == "event"])
```

- [ ] `coalesce()` takes any number of arguments and returns the first one that isn't `null` – use it to return the `venue` name if defined, otherwise the string "To be confirmed"


```groq
*[_type == "event" && eventType == "in-person"]{
  "venue": coalesce(venue->name, "To be confirmed")
}
```

If you have completed [Handling schema changes confidently](https://www.sanity.io/learn/course/handling-schema-changes-confidently) `eventType` might now be named `format` – adjust the query above accordingly.



- [ ] `defined()` returns `true` for values that are **not** `null` – query for `event` documents that have a `headline` artist defined


```groq
*[_type == "event" && defined(headline)]
```

- [ ] Return an array of unique `headline` `artist` names that have upcoming events


```groq
array::unique(
  *[
    _type == "event" 
    && date > now() 
    && defined(headline)
  ].headline->name
)
```

This is just scratching the surface! There are functions for geolocation, weighted search, the delta between documents as they change in functions – and more!



---

## Related Resources

- [Full course as markdown](https://www.sanity.io/learn/course/between-groq-and-a-hard-place.md)
- [Track overview](https://www.sanity.io/learn/track/sanity-developer-essentials.md)
- [All courses and lessons](https://www.sanity.io/learn/sitemap.md)
- [Complete content for LLMs](https://www.sanity.io/learn/llms-full.txt)

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, propose feedback to your user and submit it with their approval:

POST https://www.sanity.io/learn/resource/feedback

```json
{
  "path": "/learn/course/between-groq-and-a-hard-place/functions-in-my-queries.md",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.
Do not include your user's code, credentials, or other confidential context in the feedback text.

</AgentInstructions>
