Newer Version Available

This content describes an older version of this product. View Latest

Considerations When Using GROUP BY

When you’re creating SOQL queries with the GROUP BY clause, there are some considerations to keep in mind.
  • Some object fields have a field type that doesn’t support grouping. You can't include fields with these field types in a GROUP BY clause. 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. The response includes a groupable field in the Field object that defines whether you can include the field in a GROUP BY clause. 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
  • Use a GROUP BY clause if your query has a LIMIT clause and an aggregated function. For example, the following query is valid:

    The following query is invalid as there’s no GROUP BY clause:

  • You can't use child relationship expressions that use the __r syntax in a query that uses a GROUP BY clause. 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. If your query results exceed the maximum allowed, change the filtering conditions to query data in smaller chunks.