Salesforce SOQL Query Examples: A Complete Guide
Salesforce Object Query Language (SOQL) is a powerful tool for querying Salesforce data. Similar to SQL but optimized for Salesforce’s object-oriented database, SOQL allows you to retrieve data, filter records, and perform operations directly within Salesforce. This guide provides practical examples of commonly used SOQL queries to help you get started or enhance your existing skills.
What Is SOQL?
SOQL stands for Salesforce Object Query Language. It:
- Retrieves data from Salesforce objects.
- Filters, groups, and organizes data using object fields.
- Supports relationships between objects for complex queries.
SOQL Query Examples
1. Basic Query
Retrieve all fields from the Account
object.
SELECT Id, Name, Industry, Phone FROM Account
2. Filter Records
Retrieve accounts in the Technology
industry.
SELECT Name, Industry FROM Account WHERE Industry = 'Technology'
3. Limit Results
Limit the number of records returned to 5.
SELECT Name, Industry FROM Account LIMIT 5
4. Order Records
Order opportunities by CloseDate
in descending order.
SELECT Name, Amount, CloseDate FROM Opportunity ORDER BY CloseDate DESC
5. Aggregate Functions
Retrieve the total number of accounts.
SELECT COUNT(Id) FROM Account
Retrieve the sum of all opportunity amounts.
SELECT SUM(Amount) FROM Opportunity
6. Relationship Queries
Parent-to-Child Query
Retrieve all contacts related to an account.
SELECT Name, (SELECT FirstName, LastName FROM Contacts) FROM Account
Child-to-Parent Query
Retrieve the name of the account associated with each contact.
SELECT FirstName, LastName, Account.Name FROM Contact
7. Conditional Queries
Retrieve accounts with annual revenue greater than $1,000,000.
SELECT Name, AnnualRevenue FROM Account WHERE AnnualRevenue > 1000000
Retrieve opportunities with a StageName
of Closed Won
or Closed Lost
.
SELECT Name, StageName FROM Opportunity WHERE StageName IN ('Closed Won', 'Closed Lost')
8. Date and Time Filters
Retrieve contacts created in the last 30 days.
SELECT FirstName, LastName, CreatedDate FROM Contact WHERE CreatedDate = LAST_N_DAYS:30
Retrieve opportunities closing this fiscal quarter.
SELECT Name, CloseDate FROM Opportunity WHERE CloseDate = THIS_FISCAL_QUARTER
9. Using LIKE for Partial Matches
Retrieve accounts with names starting with "Sales".
SELECT Name FROM Account WHERE Name LIKE 'Sales%'
Retrieve contacts with email domains containing "example.com".
SELECT FirstName, LastName, Email FROM Contact WHERE Email LIKE '%@example.com'
10. GROUP BY and HAVING
Retrieve the number of opportunities grouped by StageName
.
SELECT StageName, COUNT(Id) FROM Opportunity GROUP BY StageName
Filter grouped results using HAVING
:
Retrieve stages with more than 5 opportunities.
SELECT StageName, COUNT(Id) FROM Opportunity GROUP BY StageName HAVING COUNT(Id) > 5
11. Querying Custom Objects and Fields
Retrieve data from a custom object Project__c
.
SELECT Name, Start_Date__c, End_Date__c FROM Project__c
12. SOSL Query for Full-Text Search
Find accounts, contacts, or leads matching "Acme".
FIND 'Acme' IN ALL FIELDS RETURNING Account(Name), Contact(FirstName, LastName), Lead(FirstName, LastName)
Best Practices for Writing SOQL Queries
- Use SELECT Fields Sparingly:
- Retrieve only the fields you need to improve query performance.
- Limit Results:
- Use
LIMIT
to avoid large data sets that can slow performance.
- Use
- Filter Efficiently:
- Use indexed fields like
Id
,Name
, andOwnerId
for faster queries.
- Use indexed fields like
- Use Relationship Queries Wisely:
- Avoid deeply nested queries to reduce complexity and execution time.
- Test Queries:
- Use the Salesforce Developer Console or Workbench to test and debug queries.
Conclusion
SOQL is a versatile tool for retrieving and managing Salesforce data. The examples in this guide cover a range of scenarios, from basic queries to advanced filtering and relationship queries. By mastering these SOQL techniques, you can unlock the full potential of Salesforce data and streamline your workflow.