# Aggregate Examples

AVAILABLE API VERSION |
---|

API v58.0 and later |

Use aggregate functions to analyze your query results. Aggregate functions include `avg`

, `count`

, `countDistinct`

, `min`

, `max`

, and `sum`

.

Return the average amount on all opportunities.

You can use aggregate functions without a `groupBy`

argument, such as using `totalCount`

to find the total number of query results or using the `avg`

function to find the average amount for all opportunities.

The previous query is similar to the following SOQL statement.

Using the `groupBy`

argument with aggregation functions enables you to perform further analysis on your results, like finding the average amount of all opportunities by campaign.

The previous query is similar to the following SOQL statement.

Return the total number of account records.

The previous query returns this response.

The previous query is similar to the following SOQL statement.

To use the `count`

function and group by a field, use `Id { count { value } }`

on the `aggregate`

field.

The previous query returns this response.

The previous query is similar to the following SOQL statement.

Return all accounts and their average annual revenue.

You can't simultaneously query both the `record`

and `aggregate`

fields within `RecordAggregate`

. Query the records and aggregation separately like this.

Return the average annual revenue of account records.

The previous query returns this response.

The previous query is similar to the following SOQL statement.

Return the number of industries. To return the number of distinct non-null field values matching the query criteria, use the `countDistinct`

function.

The previous query returns this response.

The previous query is similar to the following SOQL statement.

To return the maximum value of a field, use the `max`

function.

The previous query is similar to the following SOQL statement.

To return the sum of a numeric field, use the `sum`

function. This example returns the opportunity amount sum grouped by name.

The previous query is similar to the following SOQL statement.