# Aggregate Queries - Graphql

**URL:** <https://discuss.dgraph.io/t/aggregate-queries-graphql/11948>\
**Category:** Documentation\
**Created:** [December 16, 2020, 7:30pm UTC](https://discuss.dgraph.io/t/aggregate-queries-graphql/11948 "2020-12-16T19:30:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 16, 2020, 7:30pm UTC](https://discuss.dgraph.io/t/aggregate-queries-graphql/11948/1 "2020-12-16T19:30:27Z")

</div>

Dgraph automatically generates aggregate queries for GraphQL schemas. Aggregate queries fetch aggregate data, including the following:

- _Count queries_ that let you count fields satisfying certain criteria specified using a filter.
- _Advanced aggregate queries_ that let you calculate the maximum, minimum, sum and average of specified fields.

Aggregate queries are compatible with the `@auth` directive and follow the same authorization rules as the `query` keyword. You can also use filters with aggregate queries, as shown in some of the examples provided below.

### Count queries at root

For every `type` defined in a GraphQL schema, Dgraph generates an aggregate query `aggregate<type name>`. This query includes a `count` field, as well as [advanced aggregate query fields](https://dgraph.io#advanced-aggregate-queries-at-root).

 Examples 

Example: Fetch the total number of `posts`.

```
   query {
     aggregatePost {
       count
     }
   }

```

Example: Fetch the number of `posts` whose titles contain `GraphQL`.

```
   query {
     aggregatePost(filter: {
       title: {
         anyofterms: "GraphQL"
         }
       }) {
       count
     }
   }

```

### Count queries for child nodes

Dgraph also defines `<field name>Aggregate` fields for every field which is of type `List[Type/Interface]` inside `query<type name>` queries, allowing you to do a `count` on fields, or to use the [advanced aggregate queries](https://dgraph.io#advanced-aggregate-queries-for-child-nodes).

 Examples 

Example: Fetch the number of `posts` for all authors along with their `name`.

```
   query {
     queryAuthor {
       name
       postsAggregate {
        count
       }
     }
   }

```

Example: Fetch the number of `posts` with a `score` greater than `10` for all authors, along with their `name`

```
   query {
     queryAuthor {
       name
       postsAggregate(filter: {
         score: {
           gt: 10
         }
       }) {
        count
      }
    }
  }

```

### Advanced aggregate queries at root

For every `type` defined in the GraphQL schema, Dgraph generates an aggregate query `aggregate<type name>` that includes advanced aggregate query fields, and also includes a `count` field (see [Count queries at root](https://dgraph.io#count-queries-at-root)). Dgraph generates one or more advanced aggregate query fields (`<field-name>Min`, `<field-name>Max`, `<field-name>Sum` and `<field-name>Avg`) for fields in the schema that are typed as `Int`, `Float`, `String` and `Datetime`.

Note Advanced aggregate query fields are generated according to a field’s type. Fields typed as `Int` and `Float` get the following query fields: `<field name>Max`, `<field name>Min`, `<field name>Sum` and `<field name>Avg`. Fields typed as `String` and `Datetime` only get the `<field name>Max`, `<field name>Min` query fields.

 Examples 

Example: Fetch the average number of `posts` written by authors:

```
   query {
     aggregateAuthor {
       numPostsAvg
     }
   }

```

Example: Fetch the total number of `posts` by all authors, and the maximum number of `posts` by any single `Author`:

```
   query {
     aggregateAuthor {
       numPostsSum
       numPostsMax
     }
   }

```

Example: Fetch the average number of `posts` for authors with more than 20 `friends`:

```
   query {
     aggregateAuthor (filter: {
       friends: {
         gt: 20
       }
     }) {
       numPostsAvg
     }
   }

```

### Advanced aggregate queries for child nodes

Dgraph also defines aggregate `<field name>Aggregate` fields for child nodes within `query<type name>` queries. This is done for each field that is of type `List[Type/Interface]` inside `query<type name>` queries, letting you fetch minimums, maximums, averages and sums for those fields.

Note Aggregate query fields are generated according to a field’s type. Fields typed as `Int` and `Float` get the following query fields:`<field name>Max`, `<field name>Min`, `<field name>Sum` and `<field name>Avg`. Fields typed as `String` and `Datetime` only get the `<field name>Max`, `<field name>Min` query fields.

 Examples 

Example: Fetch the minimum, maximum and average `score` of the `posts` for each `Author`, along with each author’s `name`.

```
   query {
     queryAuthor {
       name
       postsAggregate {
         scoreMin
         scoreMax
         scoreAvg
       }
     }
   }

```

Example: Fetch the date of the most recent post with a `score` greater than `10` for all authors, along with the author’s `name`.

```
   query {
     queryAuthor {
       name
       postsAggregate(filter: {
         score: {
           gt: 10
         }
       }) {
         datePublishedMax
      }
    }
  }

```

### Aggregate queries on null data

Aggregate queries against empty data return `null`. This is true for both the `<field name>Aggregate` fields and `aggregate<type name>` queries generated by Dgraph.

So, in the examples above, the following is true:

- If there are no nodes of type `Author`, the `aggregateAuthor` query will return null.
- If an `Author` has not written any posts, the field `postsAggregate` will be null for that `Author`.

* * *
This is a companion discussion topic for the original entry at [https://dgraph.io/docs/graphql/queries/aggregate/](https://dgraph.io/docs/graphql/queries/aggregate/)
