Salesforce Object Query Language (SOQL) is a powerful tool that allows you to retrieve data stored in your Salesforce organization. Whether you’re creating reports, building Apex code, or automating workflows, SOQL is your go-to tool for querying Salesforce data. In this blog post, we’ll cover the basics of SOQL and how to use it effectively.
What Is SOQL?
SOQL, or Salesforce Object Query Language, is similar to SQL (Structured Query Language) but optimized specifically for Salesforce data. It enables you to query Salesforce objects like Accounts, Contacts, Opportunities, and custom objects. SOQL is used in various contexts, including Apex code, Salesforce Developer Console, and even within tools like Workbench.
SOQL allows you to:
- Retrieve specific fields from an object.
- Filter records using conditions.
- Sort and group results.
- Query data from related objects.
Basic Syntax of SOQL
The basic structure of a SOQL query is straightforward:
SELECT fields FROM object WHERE conditions ORDER BY field
- SELECT: Specifies the fields you want to retrieve.
- FROM: Indicates the object you’re querying.
- WHERE: Adds filtering conditions (optional).
- ORDER BY: Specifies how to sort the results (optional).
Let’s look at some examples to understand these components better.
Example 1: Querying All Records
To retrieve all Account records and display their names:
SELECT Name FROM Account
This query fetches the Name field from all Account records in your Salesforce org.
Example 2: Filtering Records with WHERE
You can narrow down your results using conditions in the WHERE clause. For example, to find all Accounts in the “Technology” industry:
SELECT Name FROM Account WHERE Industry = 'Technology'
Common operators for filtering include:
=(equals)!=(not equals)<,>,<=,>=LIKE(for partial matches, using wildcards like%)
Example 3: Sorting Results with ORDER BY
You can use ORDER BY to sort your query results. For instance, to get a list of Opportunities sorted by CloseDate:
SELECT Name, CloseDate FROM Opportunity ORDER BY CloseDate DESC
Use ASC for ascending order (default) and DESC for descending order.
Example 4: Querying Related Objects
SOQL allows you to retrieve data from related objects using relationship queries:
- Parent-to-Child (using subqueries):
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
This retrieves Account names and the Last Names of their related Contacts.
- Child-to-Parent:
SELECT Name, Account.Name FROM Contact
This retrieves Contact names along with their related Account names.
Example 5: Limiting Results
If you only need a subset of records, you can use the LIMIT keyword. For instance, to fetch just 5 Accounts:
SELECT Name FROM Account LIMIT 5
Best Practices for Writing SOQL
- Use SELECT Wisely: Query only the fields you need to optimize performance.
- Filter with WHERE: Narrow your results to reduce the load on Salesforce’s servers.
- Bulkify Your Code: If using SOQL in Apex, make sure your queries can handle bulk operations.
- Test in Developer Console: Use the Salesforce Developer Console to experiment with and test your SOQL queries.
- Stay Within Governor Limits: Salesforce imposes strict limits on SOQL queries, such as a maximum of 100 queries per Apex transaction. Always design queries with these limits in mind.
Using SOQL in the Developer Console
The Salesforce Developer Console is a great tool for testing your SOQL queries:
- Open the Developer Console from your Salesforce user menu.
- Go to Query Editor.
- Type your SOQL query and click Execute.
You’ll see the results displayed in a grid, making it easy to validate your queries.
Wrapping Up
SOQL is an essential skill for anyone working with Salesforce, from admins to developers.
Watch for the next blog post about SOSL; the Salesforce Search Language!

We would love to hear your comments!