# Lesson 8.2: Subqueries

The best way to do subqueries in TypeQL is with functions (though we’ll see how to use `fetch` for similar purposes in the next lesson).

## [](#_subquery_functions)Subquery functions

Let’s see how to retrieve attributes individually or grouped by owner.

Let’s try to write a query to retrieve a list of book titles along with the first contributor (chosen alphabetically), returning values sorted by title and then first contributor.

This query requires a subquery to find the first contributor alphabetically. Let’s capture this in a function

```typeql
fun first_contributor($book: book) -> { contributor }:
  match
  $contributor isa contributor, has name $name;
  authoring ($contributor, $book);
  sort $name;
  limit 1;
  return { $contributor };
```

Then, we can invoke this subquery for every book, and sort

```typeql
with fun first_contributor($book: book) -> contributor:
  match
  $contributor isa contributor, has name $name;
  authoring ($contributor, $book);
  sort $name;
  return first $contributor;
match
$book isa book, has title $title;
let $first-contributor = first_contributor($book);
$first-contributor has name $name;
sort $title, $name;
fetch {
  "title": $title,
  "contributor": $name,
};
```

The subquery functions is exactly like a regular query, except that arguments are provided from the function signature. That means that the subquery will only return contributors of that one book.

[Sorting and pagination](../8.3-sorting-and-pagination/index.md) [Structured fetching](../8.5-structured-fetching/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/academy/modules/ROOT/pages/8-composing-clauses/8.4-subqueries.adoc) Edit this page on GitHub.