Top 8 SOQL Query Examples
SOQL solutions for data-to-day problems you’ll encounter as a developer, admin, or power user. Tutorials and documentation for new and seasoned SOQL users.
- Fetch all fields with Salesforce SOQL
SELECT FIELDS(ALL) FROM Account
- Paginate Salesfore SOQL query
SELECT Id, Name FROM Contact ORDER BY Name LIMIT 200 OFFSET 200
- Salesforce SOQL wildcard search query
SELECT Id, Name FROM Contact WHERE FirstName LIKE 'Wi%'
- Date Literal Search
SELECT Id, Name, CreatedDate FROM Contact WHERE CreatedDate = TODAY
- Date Searchs
SELECT Id, ContractNumber FROM Contract WHERE EndDate < 2021-10-01
- DateTime Searches
SELECT Id, Name, CreatedDate FROM Contact WHERE CreatedDate < 2021-10-08T00:00:00Z
- Count Query
SELECT COUNT() FROM Account WHERE CreatedDate > 2021-01-01T00:00:00Z
- Count related objects
SELECT COUNT(), COUNT(ContactId) FROM Account
- Count GROUP BY
SELECT COUNT(Id), LastName FROM Contact GROUP BY LastName LIMIT 200
- Aggregate Queries (Count, Sum, Avg) with and withoug GROUP BY and HAVING
SELECT COUNT(Id), LastName FROM Contact GROUP BY LastName HAVING COUNT(Id) > 1 LIMIT 200
- Include parent field in result (child-to-parent query)
SELECT Id, Name, Account.Name FROM Contact WHERE Account.CreatedDate = LAST_MONTH
- Where clause with child field filter
SELECT Id, Name, Contact.Name FROM Account WHERE Account.Contact.Name LIKE 'a%'
- Relationship Queries (Parent-Child)
SELECT Id, Name,
(
SELECT Id, FirstName, LastName FROM Contacts
)
FROM Account WHERE Name like 'a%' - Picklist queries
SELECT Id, FirstName, LastName FROM Acccount WHERE Status includes ('OPEN;HOLD', 'PROCESSING')
- IN/NOT IN CLAUSE
SELECT FirstName, LastName FROM Account WHERE BillingState IN ('Florida', 'Texas')
- Nulls First, Nulls Last
Secrets and unsupported features
- what and who