Salesforce SOQL Query Cheat Sheet: Complete Reference for Developers
- 1 day ago
- 2 min read
SOQL (Salesforce Object Query Language) is one of the most essential skills for any Salesforce Developer or Admin. This cheat sheet covers all the key SOQL syntax, operators, relationship queries, aggregate functions, and governor limits you need for day-to-day development and certification exams.
Basic SOQL Syntax
The basic structure of a SOQL query is: SELECT field1, field2 FROM ObjectName WHERE condition ORDER BY field LIMIT n
Example: SELECT Id, Name, Industry FROM Account WHERE Industry = 'Technology' ORDER BY Name ASC LIMIT 10
SOQL Comparison Operators
= (equals), != (not equals)
< (less than), > (greater than), <= (less or equal), >= (greater or equal)
LIKE: pattern matching using % (wildcard for any characters) and _ (single character). Example: WHERE Name LIKE 'Sales%'
IN / NOT IN: check against a list. Example: WHERE Industry IN ('Technology', 'Finance')
INCLUDES / EXCLUDES: for multi-select picklist fields
Logical Operators
AND: both conditions must be true. Example: WHERE Industry = 'Technology' AND AnnualRevenue > 1000000
OR: at least one condition must be true
NOT: negates a condition. Example: WHERE NOT Industry = 'Technology'
Relationship Queries
Child-to-Parent (Dot Notation)
Traverse from a child to its parent using dot notation on the relationship field name. Example: SELECT Id, Name, Account.Name, Account.Industry FROM Contact
Parent-to-Child (Nested Subquery)
Retrieve child records in a subquery using the child relationship name (plural). Example: SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account
Aggregate Functions
COUNT(): returns the number of rows. Example: SELECT COUNT() FROM Account WHERE Industry = 'Technology'
COUNT(field): counts non-null values of a specific field
SUM(field): total sum of a numeric field. Example: SELECT SUM(Amount) FROM Opportunity
AVG(field): average of a numeric field
MIN(field) / MAX(field): minimum or maximum value
GROUP BY: groups results by a field. Example: SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry
HAVING: filter on aggregated results. Example: SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry HAVING COUNT(Id) > 10
Date and DateTime Functions
TODAY: WHERE CloseDate = TODAY
THIS_WEEK, LAST_WEEK, NEXT_WEEK
THIS_MONTH, LAST_MONTH, NEXT_MONTH
THIS_YEAR, LAST_YEAR, NEXT_YEAR
LAST_N_DAYS:n — Example: WHERE CreatedDate = LAST_N_DAYS:30
SOQL in Apex
In Apex, SOQL queries are written inline within square brackets. Example: List accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology' LIMIT 50];
Use bind variables to inject Apex variables safely into queries. Example: String ind = 'Technology'; List accs = [SELECT Id FROM Account WHERE Industry = :ind];
Key SOQL Governor Limits
Maximum SOQL queries per transaction: 100 (synchronous) / 200 (asynchronous)
Maximum records returned per query: 50,000
Maximum records retrieved via Database.getQueryLocator: 10,000,000 (used in Batch Apex)
Avoid SOQL inside loops — this is the most common governor limit violation pattern
SOQL vs SOSL
SOQL queries a single object (with relationship traversal). Use it when you know which object to query. SOSL (Salesforce Object Search Language) searches across multiple objects simultaneously using a text search. Use SOSL when you need full-text search across objects like Account, Contact, and Lead at the same time.
Conclusion
Bookmark this SOQL cheat sheet as your go-to reference for development and exam prep. Mastering SOQL is foundational to Salesforce development — almost every Apex class, Flow, and integration you build will rely on efficient data queries. Follow SFDCPi for more Apex and developer reference guides.

Comments