Skip to content

SOQL: Using Aggregate Functions

SOQL Aggregate Functions Hero Image

In our previous posts, we covered the basics of Salesforce Object Query Language (SOQL) and how to query parent and child objects. Now, let’s delve into another powerful feature of SOQL; aggregate functions. Aggregate functions allow you to perform calculations on your data, such as counting records, summing values, and finding averages. This guide will walk you through the basics of using aggregate functions in SOQL, their syntax, and some practical examples.


Aggregate functions in SOQL are used to perform calculations on a set of records and return a single value. They are particularly useful for generating reports and summaries directly from your Salesforce data. Common aggregate functions include:

COUNT(): Counts the number of records.
SUM(): Sums the values of a numeric field.
AVG(): Calculates the average value of a numeric field.
MIN(): Finds the minimum value of a field.
MAX(): Finds the maximum value of a field.
COUNT_DISTINCT(): Counts the number of unique, non-duplicate values in a specified field.


The COUNT() function is used to count the number of records that meet a certain condition. Here is a basic example of using the count function:

SELECT COUNT() FROM Contact

This query counts the number of Contact records in your Salesforce instance and will return a single number representing the total number of Contacts that the user has visibility of.

Similarly, we can count on field names:

SELECT COUNT(Id) FROM Contact

This query will essentially return the same result as all records have an Id. However if we count on Email

SELECT COUNT(Email) FROM Contact

This will likely return a completely different number as some records could be missing an Email value. In this query, only the number of contact records with a non-blank Email value will be returned.

We can also use aggregate functions to count the number of records that meet a certain condition by adding filter conditions:

SELECT COUNT() FROM Contact WHERE LastName = 'Smith'

This query counts the number of Contact records where the LastName is ‘Smith’.

The SUM() function is used to calculate the total sum of a numeric field.

Example: Summing Annual Revenue

SOQL Aggregate Function Table 1
SELECT SUM(AnnualRevenue) FROM Account WHERE Industry = 'Technology'

This query calculates the total AnnualRevenue for all Account records in the ‘Technology’ industry and will return 5,250,000.

The AVG() function calculates the average value of a numeric field.

Example: Average Annual Revenue

SELECT AVG(AnnualRevenue) FROM Account WHERE Industry = 'Technology'

This query calculates the average AnnualRevenue for all Account records in the ‘Technology’ industry e.g., 5,250,000 / 5 = 1050000.

The MIN() and MAX() functions find the minimum and maximum values of a field, respectively.

Example: Minimum and Maximum Annual Revenue

SELECT MIN(AnnualRevenue), MAX(AnnualRevenue) FROM Account WHERE Industry = 'Technology'

This query finds the minimum (850,000) and maximum (1,300,000) AnnualRevenue for all Account records in the ‘Technology’ industry.

The COUNT_DISTINCT() function counts the number of unique, non-duplicate values in a specified field.

Example: Counting Unique Industries

SELECT COUNT_DISTINCT(Industry) FROM Account

This query counts the number of unique industries in the Account records and returns that number, in this case 2.

The GROUP BY clause is used to group records by one or more fields and perform aggregate calculations on each group.

Example: Grouping by Industry

SELECT Industry, SUM(AnnualRevenue) FROM Account GROUP BY Industry

This query groups Account records by Industry and calculates the total AnnualRevenue for each industry.

SOQL Aggregate Function Table 2

Example: Grouping by Industry with HAVING

You can also use the HAVING clause to filter groups based on aggregate values.

SELECT Industry, SUM(AnnualRevenue) FROM Account GROUP BY Industry HAVING SUM(AnnualRevenue) > 1600000

This query groups Account records by Industry, calculates the total AnnualRevenue for each industry, and returns only those industries where the total AnnualRevenue is greater than 1,600,000.

SOQL Aggregate Function Table 3
  • Use Aggregate Functions Wisely: Be mindful of the performance impact when using aggregate functions, especially on large datasets. These functions can be resource-intensive and may slow down your queries.
  • Combine with WHERE: Utilise the WHERE clause to filter records before applying aggregate functions. This can significantly improve query efficiency by reducing the number of records that need to be processed.
  • Understand Query Limits: Queries that include aggregate functions are still subject to the limit on the total number of query rows. All aggregate functions, except COUNT() or COUNT(fieldname), count each row used by the aggregation as a query row for the purposes of limit tracking. For COUNT() or COUNT(fieldname) queries, limits are counted as one query row, unless the query contains a GROUP BY clause, in which case one query row per grouping is consumed.
  • Test in Developer Console: Make use of the Salesforce Developer Console or tools like Salesforce Inspector to test and refine your aggregate queries. This helps ensure that your queries are optimised and functioning as expected.

Aggregate functions in SOQL provide a powerful way to perform calculations and generate summaries directly from your Salesforce data. Once you get the hang of these functions, you’ll be able to generate more insightful reports and make smarter, data-driven decisions. The best way to get comfortable with them is to practise on different datasets, so you can really unlock their full potential in your Salesforce environment.

For further reading check out the Salesforce documentation