---
schema_version: "1"
id: adceabc13d2dfefd9acdb2dd22e15a81fb1a82b160a3f89040aa97abb9a4fa5c
title: Aggregate Functions
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-soql-sosl/guides/soql-sosl/sforce-api-calls-soql-select-agg-functions.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-soql-sosl/guide/sforce-api-calls-soql-select-agg-functions.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 291e5aa27623855d6eddb4ffa82ceaf4ec1b10e507de9727fd3ec1e731380346
products:
  - SOQL and SOSL Reference
  - Platform Developer Content
---

# Aggregate Functions

Use aggregate functions in a `GROUP BY` clause in SOQL queries to generate reports for analysis. Aggregate functions include `AVG()`, `COUNT()`, `MIN()`, `MAX()`, `SUM()`, and more.

You can also use aggregate functions _without_ using a `GROUP BY` clause. For example, you could use the `AVG()` aggregate function to find the average `Amount` for all your opportunities.

<!-- otherprops: set_clipboard -->

```soql
SELECT AVG(Amount)
FROM Opportunity
```

However, these functions become a more powerful tool to generate reports when you use them with a `GROUP BY` clause. For example, you could find the average `Amount` for all your opportunities by campaign.

<!-- otherprops: set_clipboard -->

```soql
SELECT CampaignId, AVG(Amount)
FROM Opportunity
GROUP BY CampaignId
```

This table lists all the aggregate functions supported by SOQL.

**note:** All aggregate functions ignore null values, except for `COUNT()` and `COUNT(Id)`.

`COUNT(fieldname)` is different from `COUNT()`. `COUNT(fieldname)` does ignore null values.

| Aggregate Function               | Description                                                                                                                                                                                   |
| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AVG()`                          | Returns the average value of a numeric field.                                                                                                                                                 |
| `COUNT()` and `COUNT(fieldName)` | Returns the number of rows matching the query criteria.                                                                                                                                       |
| `COUNT_DISTINCT()`               | Returns the number of distinct non-`null` field values matching the query criteria.                                                                                                           |
| `MIN()`                          | Returns the minimum value of a field. If you use the `MIN()` or `MAX()` functions on a picklist field, the function uses the sort order of the picklist values instead of alphabetical order. |
| `MAX()`                          | Returns the maximum value of a field. Available in API version 18.0 and later.                                                                                                                |
| `SUM()`                          | Returns the total sum of a numeric field.                                                                                                                                                     |

Example `AVG()`:

<!-- otherprops: set_clipboard -->

```soql
SELECT CampaignId, AVG(Amount)
FROM Opportunity
GROUP BY CampaignId
```

Example using `COUNT()`:

<!-- otherprops: soql -->

```codesegment
SELECT COUNT()
FROM Account
WHERE Name LIKE 'a%'
```

Example using `COUNT(fieldName)`:

<!-- otherprops: soql -->

```codesegment
SELECT COUNT(Id)
FROM Account
WHERE Name LIKE 'a%'
```

**Note:** `COUNT()` and `COUNT(Id)` in SOQL are similar to `COUNT(*)` in SQL.

Example `COUNT_DISTINCT()`:

<!-- otherprops: set_clipboard -->

```soql
SELECT COUNT_DISTINCT(Company)
FROM Lead

```

**Note:** `COUNT_DISTINCT(fieldName)` in SOQL is equivalent to `COUNT(DISTINCT fieldName)` in SQL. .

Example `MIN()`:

<!-- otherprops: set_clipboard -->

```soql
SELECT MIN(CreatedDate), FirstName, LastName
FROM Contact
GROUP BY FirstName, LastName
```

Example `MAX()`:

<!-- otherprops: set_clipboard -->

```soql
SELECT Name, MAX(BudgetedCost)
FROM Campaign
GROUP BY Name
```

Example `SUM()`:

<!-- otherprops: set_clipboard -->

```soql
SELECT SUM(Amount)
FROM Opportunity
WHERE IsClosed = false AND Probability > 60
```

You can't use a `LIMIT` clause in a query that uses an aggregate function, but does not use a `GROUP BY` clause. For example, the following query is invalid:

<!-- otherprops: set_clipboard -->

```soql
SELECT MAX(CreatedDate)
FROM Account LIMIT 1
```
