Warning (Debug Log): SOQL query with no filter returns all rows — this may return more records than expected
What does this error mean?
A SOQL query without a WHERE clause retrieves every record on the queried object. In development with small data sets this seems harmless, but in production orgs with hundreds of thousands of records, it will breach the 50,000-row governor limit, slow down the transaction significantly, or trigger the non-selective query error on large objects. This warning appears in debug logs and is surfaced by code review tools and Salesforce's Best Practice Analyzer.
Even with a LIMIT clause, a filterless query performs a full table scan internally — retrieving and discarding rows until it reaches the limit. The query overhead is the same regardless of how small the LIMIT is.
Common Causes
1. Development queries promoted to production
Quick queries written during development or debugging (SELECT Id FROM Account LIMIT 10) accidentally left in production code.
2. Utility methods that retrieve all records of a type
Helper methods like getAllAccounts() that were designed for small orgs and never had filters — fine in sandbox, dangerous in production at scale.
3. Admin scripts run via Execute Anonymous
One-off data operations that use broad queries to get all records of a type, then process them — workable for small objects, will fail for large ones.
How to Fix It
Solution 1: Always add a contextual WHERE clause
// ❌ BAD — retrieves all accounts
List<Account> allAccounts = [SELECT Id, Name FROM Account];
// ✅ GOOD — scoped to relevant context
List<Account> activeAccounts = [
SELECT Id, Name
FROM Account
WHERE IsActive__c = true
AND OwnerId = :UserInfo.getUserId()
LIMIT 1000
];
Solution 2: For large-data processing, use Batch Apex
If you genuinely need to process all records of an object, use Batch Apex with Database.getQueryLocator(). It handles pagination safely and doesn't hit the 50,000-row limit per chunk.
Code review rule: Treat any SOQL query without a WHERE clause as a bug in production code, not just a warning. Add a WHERE clause or a // intentionally no filter: [reason] comment so future reviewers don't question it. Static analysis tools like PMD ApexRules flag these automatically.