soql – Fetch Accounts having more than 3 contact records in it in a single query

I want to Fetch all the accounts records(not the ids) that have 3 or more contacts present in a single query with contact records in it.

I didn’t find any reference of these.

I had tried with some of the feature with that we can do but there I had to iterate through the collections. But I don’t want that.

List<Account> acList = [SELECT Id, Name, (SELECT ID, Name FROM Contacts) FROM Accounts];

List<Account> selectedAccounts = new List<Account>();

for(Account ac: acList){
     if(ac.Contacts.size() > 3){
         selectAccounts.add(ac);
     }
}

But this solution I don’t want ^^^

Neither do I want this
select accountid from contact group by accountid having count(id) > 3

Can anyone please advice as how to get the records in any other way

Read more here: Source link