System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop

In order to avoid System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop, make sure the sub query is limited. If the sub query returns more records, we get System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop exception.

Sample Incorrect Code:

for ( Account acc : [ SELECT Id, (SELECT Id FROM Contacts) FROM Account ] ) {
}

Correct Code:

List < Account > lstAcct = [ SELECT Id, (SELECT Id FROM Contacts LIMIT 200) FROM Account ];

for ( Account acc : lstAcct ) {
}

Cheers!!!