Skip to main content

Unbound SOQL Statement

Flags SOQL queries without WHERE or LIMIT clauses that risk breaching governor limits, causing performance degradation and runtime errors.

David Martin avatar
Written by David Martin
Updated yesterday

Overview

This rule flags SOQL statements that are not constrained by WHERE clauses or LIMIT filters, making them “unbound.” An unbound SOQL statement retrieves all records from an object, regardless of volume, which can easily breach governor limits and degrade performance.

Code reviews classifies this as an Error because unbound queries are unsafe, particularly in production environments, and can lead to CPU timeouts, heap size issues, or runtime exceptions, especially when executed in loops or automations.


Why This Matters

Unbound SOQL statements:

  • Pull excessive data into memory, risking heap size and CPU time limits

  • Violate bulk and query best practices

  • Slow down user and system operations

  • Can fail unpredictably in larger orgs or after data growth

Salesforce imposes limits on total rows returned per transaction (50,000), and unbound queries risk breaching these thresholds.

What Triggers This Rule

This rule triggers when an Apex SOQL query statement does not include a WHERE clause OR a LIMIT clause. It's designed to ensure that all SOQL queries are bound by at least one of these mechanisms to prevent excessive data retrieval. The rule also intelligently skipsabort()s analysis for test classes to avoid flagging intentional unbound queries often used for testing setup.

Example of a Violation

// ❌ Unbound query

List<Contact> contacts = [

`SELECT Id, Email`  `FROM Contact`

];

Recommended Approach

To efficiently handle large datasets, customers can leverage Batch Apex, processing records in batches of up to 200. This approach is especially recommended when setting a simple LIMIT or WHERE clause isn't sufficient for managing the data volume.


Summary

Unbound SOQL statements are unsafe and unscalable. Always filter your queries using WHERE and LIMIT clauses to control the volume of returned data. Code reviews flags this as an Error to prevent performance risks and enforce governor-limit-safe coding practices.

Did this answer your question?