Considerations for GROUP BY
Understand the special behavior and limitations for using the GROUP BY clause in SOQL queries.
-
Some object fields have a field type that doesn’t support grouping. You can’t include fields with these field types in a
GROUP BYclause.To check whether a field supports grouping, you can use SOAP API or REST API to make a describe call for the object that contains the field. For each field, the response includes a boolean
groupableattribute that defines whether you can include the field in aGROUP BYclause. To check whether a field supports grouping for standard objects, you can also check the properties for the field in the Object Reference for Salesforce and Lightning Platform. -
You must use a
GROUP BYclause if your query has aLIMITclause and an aggregated function. For example, the following query is valid:The following query is invalid as there’s no
GROUP BYclause: -
You can’t use child relationship expressions that use the
__rsyntax in a query that uses aGROUP BYclause. For more information, see Understanding Relationship Names, Custom Objects, and Custom Fields. -
In SOAP API, queries that include a GROUP BY clause can’t use the queryMore() call to get more results.
-
In REST API, queries that include a GROUP BY clause can’t use the query locator to get more results.
-
Formula fields can’t be grouped.