---
title: "SOQL for loops"
url: "https://blog.beyondthecloud.dev/tips/soql-for-loops"
category: "SOQL"
published: "2024-07-23"
---

# SOQL for loops

SOQL for loops retrieve all sObjects, using efficient chunking with calls to the query and queryMore methods of SOAP API. Developers can avoid the limit on heap size by using a SOQL for…

### SOQL For Loops

As we can read in the documentation:

SOQL for loops retrieve all sObjects, using efficient chunking with calls to the query and queryMore methods of SOAP API. Developers can avoid the limit on heap size by using a SOQL for loop to process query results that return multiple records.

It’s worth mentioning that the approach can increase the total heap size.

```apex
List<Account> accounts = [SELECT Id, Name FROM Account];

for (Account acc : accounts) {
    // ...
}
```

```apex
for (Account acc : [SELECT Id, Name FROM Account]) {
    // ...
}
```
