---
schema_version: "1"
id: 141bc6af70de770f7aea5c30b018eeff95de64c7311d672de727062b1462bedd
title: "ApexGuru Antipattern: SOQL in a Loop"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/apexguru_antipattern_soql_in_loop.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.apexguru_antipattern_soql_in_loop.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: e58c8a36bb274450fa1ec2e7bec5b861f5e8ae1c2afe270bbb1675060bbe4637
description: Executing SOQL queries inside loops can quickly exhaust the governor limits and decrease system performance. Salesforce enforces a limit on the number of queries per transaction. Instead, batch SOQL queries outside of loops.
products:
  - Cross Cloud
release: "264"
---
# ApexGuru Antipattern: SOQL in a Loop

Executing SOQL queries inside loops can quickly exhaust the governor limits and decrease system performance. Salesforce enforces a limit on the number of queries per transaction. Instead, batch SOQL queries outside of loops.

## Scope

Detection only

## Detection Example

```java
apex
for (Integer i = 0; i < 10; i++) {
      List<Account> accounts = [SELECT Id FROM Account WHERE Name = :someValue];
  }
```

#### See Also

- [SOQL For Loops in the Apex Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm)