How to count the child records for each parent record in Salesforce?

How to count the child records for each parent record in Salesforce?

Using aggregate SOQL, we can find the number of child records for each parent record in Salesforce.
Check the following sample SOQL. Since AccountId is grouped by, it will find the number of contact records for each Account Record.
Note:
Please add proper filters to the SOQL to make it selective.

Sample SOQL:



SELECT AccountId, COUNT(Id) ContactCount FROM Contact GROUP BY AccountId


Output:

Leave a Reply