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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.