Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Group__c has a lookup relationship to Account. What should be the query to fetch the number of Account records which have more then N Group__c child records?
N can be anything like 3,5 etc.
You can use the HAVING clause and an aggregate query:
Integer matchingAccounts = [
SELECT count(Id) records, Account__c FROM Group__c
GROUP BY Account__c HAVING count(Id) > :numberOfChildren
Required, but never shown