# Lesson 8: Structuring query results

## [](#_lesson_8_1_chaining_clauses)[Lesson 8.1: Chaining clauses](../8.1-chaining-clauses/index.md)

*   Clauses can be chained arbitrarily into query **pipelines**, since they all consume streams of concept rows and produce stream of concept rows
    
    ```typeql
    match <pattern>
    reduce <operation>
    match <pattern>
    sort <vars>;
    insert <pattern>
    delete <pattern>
    match <pattern>
    limit N;
    ...
    ```
    
*   `fetch` clauses can only be placed at the end of a query pipeline to transform the stream into a stream of JSON documents
    

## [](#_lesson_8_3_aggregations)[Lesson 8.3: Aggregations](../8.2-aggregations/index.md)

*   Use `reduce` operators to aggregate streams
    
*   The reduce operators are `count`, `sum`, `max`, `min`, `mean`, `median`, and `std`. Except for `count`, each modifier takes a variable to aggregate over as an argument, which can represent either a numeric attribute or a numeric value.
    
*   You can group reductions by using `groupby`
    
    ```typeql
    reduce $count = count groupby $book;
    ```
    

## [](#_sorting_and_pagination)[Sorting and pagination](../8.3-sorting-and-pagination/index.md)

*   The query streams can be sorted using the `sort` operator
    
    ```typeql
    match <pattern>
    sort var;
    ```
    
*   The results of queries can be **paginated** using the `limit` and `offset` modifiers, which take a literal integer as an argument.
    
    ```typeql
    match <pattern>
    offset 10;
    limit 5;
    ```
    
*   The `sort`, `limit`, and `offset` modifiers can all be used individually or in any combination.
    

## [](#_subqueries)[Subqueries](../8.4-subqueries/index.md)

*   Subqueries can be encapsulated and invoked easily wth functions.
    

## [](#_lesson_8_5_structured_fetching)[Lesson 8.5: Structured fetching](../8.5-structured-fetching/index.md)

*   Fetch clauses can project attributes
    
    ```typeql
    fetch {
     "value": $owner.attribute-type,
    };
    ```
    
*   Attributes, values, and types can be fetched directly in the fetch clause
    
    ```typeql
    fetch {
      "attribute": $attribute,
      "type": $type_,
      "computed-value": $computed-value,
    };
    ```
    
*   Subqueries can be written directly into the fetch clause with the correct encapsulation:
    

fetch {
  "attached-values": \[ match ...; fetch { ... }; \],
};

*   Underlying idea: all leaves in a fetch clause structure must be serializeable to a value
    

## [](#_further_learning)Further learning

[Lesson 9: Modeling schemas](../../9-modeling-schemas/index.md)

Learn how to design schemas for TypeDB using the conceptual PERA model, including common design patterns and pitfalls.

[Lesson 10: Using inference](../../10-using-functions/index.md)

Learn how to use functions to abstract complex patterns into simple forms, and capture complex logic.

[Structured fetching](../8.5-structured-fetching/index.md) [9\. Modeling schemas](../../9-modeling-schemas/index.md)

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