SAVE UP to ₹4999/- OFF On All Salesforce Courses Claim Offer

6

SOQL Aggregate Query

The Salesforce Object Query Language is powerful and allows you to access, manipulate and query your Salesforce data. Any calculations that you may want to perform on data sets, are usually done with the help of SOQL Aggregate Query. 

There are various SOQL Aggregate functions such as SUM() and MAX() which allow you to roll up and summarize your data in a query. To use aggregate functions you have to apply GROUP BY clause in SOQL queries to generate reports for analysis. Any query that includes an SOQL aggregate function returns its results in an array of AggregateResult objects.

SOQL Aggregate Functions

The table below list all the SOQL Aggregate Functions:

Aggregate FunctionDescription
AVG()Returns the average value of a numeric field.
For example: SELECT CampaignId, AVG(Amount) FROM Opportunity GROUP BY CampaignId
COUNT() and COUNT(fieldName)Returns the number of rows matching the query criteria.
For example using COUNT():SELECT COUNT() FROM Account WHERE Name LIKE ‘a%’
For example using COUNT(fieldName):SELECT COUNT(Id) FROM Account WHERE Name LIKE ‘a%’
COUNT_DISTINCT()Returns the number of distinct non-null field values matching the query criteria.
For example:SELECT COUNT_DISTINCT(Company) FROM Lead
Note:COUNT_DISTINCT(fieldName) in SOQL is equivalent to COUNT(DISTINCT fieldName) in SQL.
MIN()Returns the minimum value of a field.
For example:SELECT MIN(CreatedDate), FirstName, LastName FROM Contact GROUP BY FirstName, LastName
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.
For example:SELECT Name, MAX(BudgetedCost) FROM Campaign GROUP BY NameAvailable in API version 18.0 and later.
SUM()Returns the total sum of a numeric field.
For example:SELECT SUM(Amount) FROM Opportunity WHERE IsClosed = false AND Probability > 60

Limitations of SOQL Aggregate Query

SOQL aggregate queries have several limitations. Here we have listed some important ones:

  1. It is not possible to use aggregate functions in subqueries or nested inquiries.
  2. Only one object can be queried at a time using aggregate queries. 
  3. There are only a few aggregate functions available, including MIN(), MAX(), AVG(), SUM(), and COUNT().
  4. You cannot use group by fields or include fields that are not included in the aggregate functions while using GROUP BY.
  5. There are only 2000 records in the result set. Use the LIMIT clause if the query returns more results.
  6. Because aggregate searches naturally group the results, you cannot use ORDER BY with them.
  7. Some types of fields cannot be combined. For instance, formula fields do not allow for aggregation. Salesforce’s governor limitations, such as the total query row limit and CPU time limits, still apply to aggregate queries.
salesforce-developer
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!