
In the previous chapter, we laid the foundation by introducing you to the fundamental concepts of SOQL and setting up your Salesforce environment. Now, we’re ready to dive deeper into crafting basic SOQL queries. In this chapter, we’ll explore how to retrieve single and multiple objects, filter data using the WHERE clause, sort results with ORDER BY, limit the number of records returned, and traverse relationships between objects.
Retrieving Single and Multiple Objects
At its core, SOQL is all about querying data. Let’s start by learning how to retrieve data from a single object. Suppose you want to retrieve information about various accounts in your Salesforce org. Here’s a simple query to achieve that:

In this query:
- SELECT: We specify the fields we’re interested in retrieving (
Id
,Name
, andIndustry
). - FROM: We indicate that we’re querying the
Account
object. - WHERE: No WHERE clause is specified in this example, so all account records will be retrieved.
- ORDER BY and LIMIT: These are also absent, so the results won’t be sorted or limited.
To retrieve data from multiple objects in a single query, you can use a semi-colon to separate the queries. Each query retrieves data from a different object. For instance:

This retrieves the first and last names of contacts as well as the name and stage name of opportunities.
Filtering Data with WHERE Clause
Filtering data is a crucial aspect of querying. The WHERE
clause allows you to specify conditions that records must meet to be included in the query results. Suppose you want to retrieve contacts who are in the “Prospect” stage:

In this query:
- WHERE: We specify the condition
StageName = 'Prospect'
, which filters contacts based on their stage.
You can use various operators in your conditions, such as =
, !=
, >
, <
, >=
, and <=
. You can also combine conditions using AND
and OR
operators for more complex filtering.
Sorting Results using ORDER BY
Sorting the results helps you organize data for better analysis and presentation. The ORDER BY
clause is used to sort the query results based on specified fields. Suppose you want to retrieve contacts sorted by last name in descending order:

In this query:
- ORDER BY: We specify
ORDER BY LastName DESC
to sort contacts by last name in descending order.
You can sort by multiple fields, use ascending (ASC
) or descending (DESC
) order, and even use expressions or functions for sorting.
Limiting Records with LIMIT
Sometimes, you might only need a subset of the available records. The LIMIT
clause allows you to restrict the number of records returned by a query. Suppose you want to retrieve the first five opportunities:

In this query:
- LIMIT: We specify
LIMIT 5
to retrieve only the first five records.
Limiting results is particularly useful when dealing with large datasets, as it reduces the amount of data returned and can improve query performance.
Relationship Queries (Parent-Child)
Salesforce’s data model often involves relationships between objects. SOQL allows you to traverse these relationships and retrieve related data. Let’s explore two types of relationships: parent-child and child-parent.
Parent-Child Relationship
A parent-child relationship exists when one object is related to another through a lookup or master-detail relationship. Suppose you want to retrieve contacts along with their associated account names:

In this query:
- SELECT: We retrieve
FirstName
andLastName
from theContact
object, as well as theName
field from the relatedAccount
object. - Account.Name: This syntax accesses the
Name
field of the relatedAccount
object.
Child-Parent Relationship
A child-parent relationship exists when an object is related to another as a child. For example, an opportunity is a child of an account. To retrieve opportunities along with their associated account names:

In this query:
- SELECT: We retrieve
Name
andAmount
from theOpportunity
object, as well as theName
field from the relatedAccount
object.
Conclusion
Congratulations! You’ve now mastered the basics of SOQL queries. You’ve learned how to retrieve data from single and multiple objects, filter results using the WHERE
clause, sort data with ORDER BY
, and limit the number of records returned using LIMIT
. Additionally, you’ve delved into relationship queries, both in the parent-child and child-parent contexts.
These foundational concepts form the backbone of your SOQL knowledge. As we progress through this guide, we’ll continue to build upon these principles and explore more advanced techniques. In the next chapter, we’ll take your SOQL skills up a notch by delving into intermediate techniques, including working with aggregate functions, grouping results, and employing subqueries to achieve more sophisticated data retrieval and analysis. Get ready to elevate your SOQL expertise and unlock even more of Salesforce’s data manipulation potential!

About the blog
SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:
Newsletter
Subscribe to our email newsletter to be notified when a new post is published.