9
List<Account> acc=[select Name , (Select LastName from contacts) from Account];

How many maximum contacts can be reterived here?

4 Answers 4

8

There is no such limit for inner query. 50000 records per transaction will be applied here.

For example your org contain only 2 Account and each have 10 Contacts.

If you execute this with sub Query like below

Select Id, Name, (Select Id, Name from Contacts) From Account

Then the total number of Query rows will be 22 (2+10+10).

8

So my experience is that it varies on the number of fields retrieved in the subquery and what you are doing with the retrieved data.

Short answer: 200 Contacts for the code below:

for (Account acct : [SELECT Id, Name, (SELECT Id, Name FROM Contacts)
                    FROM Account WHERE Id IN ('<ID value>')]) {
    if (acct.Contacts.size()>0){
       doSomethingWithContacts(acct.Contacts);
    }

}

There's a somewhat cryptic comment in the SFDC documentation here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm

"You might get a QueryException in a SOQL for loop with the message Aggregate query has too many rows for direct assignment, use FOR loop. This exception is sometimes thrown when accessing a large set of child records (200 or more) of a retrieved sObject inside the loop, or when getting the size of such a record set. For example, the query in the following SOQL for loop retrieves child contacts for a particular account. If this account contains more than 200 child contacts, the statements in the for loop cause an exception."

I've tested this a bunch and have found that the actual limit varies by the number of fields retrieved in the subquery. I have moved away from subqueries quite a bit after running into this exception and have done a second query and build a map to get the same results. 200 records in a database doesn't seem line "a large set" to me. Doing a for loop on the inner list does help but it never let me get to something I would be more likely to rate "a large set of child records".

4
  • You can still query them the thing is you can't direct assign them. Both are different. Commented Jul 14, 2017 at 5:10
  • 1
    or check the size of the return, both limitations are pretty severe. It implies that you can't pass of the list and have it processed by a subroutine, which often makes for bad code. That's why I started with depends "what you are doing with the retrieved data." Also see salesforce.stackexchange.com/questions/122019/…
    – Jochen
    Commented Jul 17, 2017 at 14:25
  • 1
    That's not a query limitation. It's a limitation on the way the results can be processed when using a SOQL for loop, like this: for (Account acct : [SELECT Id, Name, (SELECT Name FROM Contacts) FROM Account]){ ... } Commented Oct 2, 2018 at 13:52
  • Technically absolutely correct. There are times when that makes sense, and there are times when you have existing subroutines to send the data too. So in real life, at least in mine, it depends on what you want to do with the results.
    – Jochen
    Commented Oct 9, 2018 at 18:28
3

Total number of records retrieved by SOQL queries:50,000

Total number of records retrieved by Database.getQueryLocator:10,000

In a SOQL query with parent-child relationship subqueries, each parent-child relationship counts as an extra query. These types of queries have a limit of three times the number for top-level queries. The row counts from these relationship queries contribute to the row counts of the overall code execution.

0

As such there is no specific limit for the number inner query records in SOQL.

However, there is one catch to the 200 sub query limit that that it causes an error while assignment directly from acc[i].contacts to any contacts list.

Although that can be handled using a for loop for assignment .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .