Salesforce developers often need to retrieve data from multiple related objects in a single query. This is where relationship queries in SOQL (Salesforce Object Query Language) become very important.
SOQL is similar to SQL, but it is specially designed for the Salesforce platform to work with objects and their relationships.
Using relationship queries, we can fetch data from parent and child objects without writing multiple queries, thereby improving performance and making the code more efficient.
Salesforce Object Query Language (SOQL) is used to retrieve data from the Salesforce database based on specified conditions and objects.
When we have two objects in a relationship in Salesforce, such as a lookup or master-detailed relationship, to retrieve data from a parent object or child object, we need to understand relationships in SOQL to build a query.
In this tutorial, we will learn about Salesforce relationship queries in SOQL (Salesforce Object Query Language).
In that, I will explain SOQL relationship queries, child-to-parent and parent-to-child relationship queries, and SOQL multilevel relationship queries, with examples.
What are Relationship Queries in SOQL?
The SOQL relationship queries allow us to retrieve data from related Salesforce objects in a single query. It can query relationships between objects and efficiently retrieve data for child or parent objects.
Relationship queries involve at least two objects: a parent and a child. These queries are used to fetch data either from the parent object when the SOQL query is written on the child object, or from the child object when the SOQL query is written on the parent object.
Example:
- Account → Parent object
- Contact → Child object
If we want:
- Account details from Contact → Child-to-Parent
- Contact details from Account → Parent-to-Child
This is the core concept of relationship queries.
Different Types of SOQL Relationship Queries
There are mainly 3 types of relationship queries:
- Child-to-Parent Relationship Queries:
- Parent-to-Child Relationship Queries:
- Multilevel Relationship in SOQL:
Child-to-Parent Relationship Queries in SOQL
The child-to-parent relationship queries in Salesforce Object Query Language (SOQL) allow us to retrieve parent object fields or information from a child object in a single query.
This type of query uses the parent object’s relationship name, accessed via dot notation.
In a child-to-parent relationship query, we start a query with the child object and then use the parent relationship name, which is the API name of the lookup or master-detail field, to access fields in the child object.
Syntax: Declaring Child-to-Parent Relationship Queries
SELECT Child_Field1, Child_Field2, Parent_Object.Parent_Field1, Parent_Object.Parent_Field2 FROM Child_ObjectExample: Child-to-Parent Relationship Queries in SOQL
Below, I have explained how to use child-to-parent relationship queries in SOQL to retrieve parent information within a child query.
In a Salesforce organization, we have a Contact object (child) with a lookup relationship to the Account object (parent), or to any other Salesforce object in a relationship with it.
For example, we want to display contact records, and with this information, we also want to display the account priority and industry field values from their parent objects.
SELECT FirstName, LastName, Account.Industry, Account.CustomerPriority__c FROM ContactIn the above SOQL query, we first entered the contact object (child) fields that we wanted to display.
After that, we need to provide object_name dot notation and the field name we want to retrieve to retrieve the information or fields from the parent object. Then, using the FROM clause, provide the child object name.
In the query results below, we can see the columns we provided to retrieve data from the contact (child) and its account (parent).

In this way, we can utilize child-to-parent relationship queries in SOQL to retrieve parent information within a child query.
Parent-to-Child Relationship Queries in SOQL
The parent-to-child relationship queries in SOQL allow us to retrieve relative child object fields or information for a parent object in a single query. This is achieved using an SOQL subquery.
In the parent query, we need to use the child query as a subquery to retrieve the details of the child records. After closing the subquery, we must continue the parent query.
Syntax: Declaring Parent-to-Child Relationship Queries
SELECT Parent_Field1, Parent_Field2, ( SELECT Child_Field1, Child_Field2 FROM Child_Object_Name ) FROM Parent_ObjectIn the first select clause, we need to provide parent object fields. Then, in the parentheses, we need to provide a subquery to retrieve the fields from the child object.
To do this, we must use the child object’s relationship name (in the plural form) within the subquery. After the subquery for child records, we need to specify the parent object name again in the FROM clause.
Example: Parent-to-Child Relationship Queries in SOQL
Below, I have explained how to use parent-to-child relationship queries in SOQL to retrieve the child object’s information within a parent query.
In Salesforce, contact and case objects are in a lookup relationship. When a case (child) is created there, we have a field to select the associated contact (parent) name.
For example, if we want to display/retrieve contact records along with related cases, then we can use parent-to-child relationship queries in SOQL.
SELECT FirstName, LastName, Department, ( SELECT CaseNumber, Status FROM Cases ) FROM ContactIn the above SOQL query, we first specified the contact (parent) object fields we wanted to retrieve.
After that, we provided a subquery inside the main query in which we retrieved all cases (child) records related to each contact. Then, using the FROM clause, provide the contact (patent) object name.
In the query results below, you can see the columns where we provided fields to retrieve data from the contact (parent) object.
Then, within this query, we used a subquery to retrieve records from the case (child) object, which displays the related records of its parent object.

In this way, we can use parent-to-child relationship queries in SOQL to get the child’s record information on a parent query.
Multilevel Relationship in SOQL
In SOQL, multilevel relationships enable us to traverse multiple relationships between objects within a single query. Using this relationship, we can retrieve both parent-to-child and child-to-parent data.
Using SOQL, we can traverse these relationships through dot notation and access fields across multiple related objects in a single query, even when there are relationships between objects, whether LR or MDR.
Now, let’s explore how multilevel relationships work in SOQL using the following examples.
Child-to-Parent-to-Parent Relationship Queries in SOQL
Child-to-parent-to-parent relationship queries in SOQL traverse the relationship from a child object to its parent, then to the grandparent.
This is useful when we want to retrieve data from a parent and a grandparent object based on a query that starts from the child object.
When traversing from a child to a parent relationship in a SOQL query, use dot notation (.), and reference the parent relationship by its relationship name, not by the field API name.
Syntax: Declaring Child-to-Parent-to-Parent Relationship Queries
SELECT Child_Fields, Parent_Fields, Grand_Parent_Fields FROM Child_ObjectExample: Child-to-Parent-to-Parent Relationship Queries
For example, we want to retrieve records from contacts and, along with them, display the associated account record. Additionally, we want to display the parent account using a SOQL query in Salesforce.
SELECT FirstName, LastName, Account.Name, Account.Parent.Name FROM ContactAs you execute the query in the image below, we can see the contact (child) and its associated account (parent) records.
However, the SOQL query in the query editor cannot fetch the grandparent records related to the contact records.

Let’s understand this by using the query in the Apex program and displaying its results with a debug statement.
In the Apex code below, we created a list to store the contacts retrieved from the SOQL query. In the query, we used a multilevel relationship query that fetches records from the child object, then its associated parent, and then the grandparent of the child object.
In the SOQL query below, the SELECT clause first fetches contact (child) records, then their associated accounts (parent) records, and finally fetches the related parent accounts (grandparent) of the contact, allowing us to establish the multilevel relationship between records.
With this, we can easily get the parent accounts of the contacts and also the parent accounts of those accounts.
Then, we need to use a for loop to iterate over the list of records we stored. Then, we created a contact object instance to fetch the values and display them in the debug method.
List<Contact> conList = [ SELECT FirstName, LastName, Account.Name, Account.Parent.Name
FROM Contact WHERE Account.Parent.Name != NULL ];
for ( Contact c : conList ) {
System.debug( ' Name :' + c.FirstName +' '+ c.FirstName +
' Account Name :' + c.Account.Name +
' Parent Account :' + c.Account.Parent.Name );
}After executing the code in the query result, you can see that we get contact (child) records, associated accounts (parent) records, and parent accounts (grandparent) of the contact object.

In this way, we can use multilevel relationships to support child-to-parent-to-parent queries in SOQL.
Parent-to-Child-to-Child Relationship Queries in SOQL
Parent-to-child-to-child relationship queries in SOQL traverse the relationship from a parent object to its related child object’s records, then to the grandchild object.
This is useful when we want to retrieve data from a child or grandchild object based on a query that starts from the parent object.
First, we must start from parent object fields in the SELECT clause and retrieve related child records using a subquery. Then, retrieve the record of a grandchild by including a subquery within the child query for another related child object.
Syntax: Declaring Parent-to-Child-to-Child Relationship Queries
SELECT Parent_Field1, Parent_Field2,
( SELECT Child_Field1, Child_Field2,
( SELECT Grand_Child_Field1, Grand_Child_Field2 FROM GrandChildRelationshipName )
FROM ChildRelationshipName )
FROM ParentObjectExample: Parent-to-Child-to-Child Relationship Queries in SOQL
For example, we want to retrieve data for accounts along with their related contacts, and then display the associated cases for those contacts and accounts.
That means here, we want to display the parent (account) record, then its related child (contact), and then the grandchild (cases).
In the below SOQL query, first, we started with the account (parent) object records -> then we also wanted to display related contacts, so before completing the account query, we added a subquery in which we entered query for retrieve contact (child) object fields then in the child query we entered query for retrieve cases (grandchild) object fields and complete the last child object query.
After that, complete the contact object subquery and, finally, the account (parent) query. In the for loop, we need to create an instance of each object used in the SOQL query and use the debug method to display the records.
List<Account> accList = [SELECT Name,
(SELECT FirstName, LastName,
(SELECT CaseNumber, Status FROM Cases WHERE CaseNumber != NULL)
FROM Contacts)
FROM Account ORDER BY Name];
for (Account acc : accList) {
for (Contact con : acc.Contacts) {
for (Case cs : con.Cases) {
System.debug('Account Name : ' + acc.Name + ' '+
' Contact Name: ' + con.FirstName + ' ' + con.LastName + ' '+
' Case Number: ' + cs.CaseNumber + ', Status: ' + cs.Status);
}
}
}In the query result, you can see that we retrieved account (parent) records, related contacts (child) records, and cases (grandchild) of the account object.

In this way, we can use multilevel relationships to support parent-to-child-to-child queries in SOQL.
Difference Between Child-to-Parent and Parent-to-Child
| Feature | Child-to-Parent | Parent-to-Child |
|---|---|---|
| Direction | Child → Parent | Parent → Child |
| Syntax | Dot notation | Subquery |
| Records | Single parent | Multiple children |
| Example | Contact → Account | Account → Contacts |
Frequently Asked Questions
Q1: What is a relationship query in SOQL?
It is used to fetch data from related objects
Q2: What is a child-to-parent query?
Fetch parent data from the child object
Q3: What is a parent-to-child query?
Fetch child records from the parent object
Q4: Can we use joins in SOQL?
No direct joins, but relationship queries work like joins
Conclusion
Relationship queries in SOQL are one of the most powerful features in Salesforce. They allow developers to retrieve related data efficiently without writing multiple queries.
Understanding child-to-parent, parent-to-child, and multi-level relationships is very important for building real-time Salesforce applications.
By using proper syntax, avoiding mistakes, and following best practices, you can write optimized SOQL queries that improve performance and scalability.
If you are preparing for Salesforce interviews or working on real projects, mastering relationship queries will help you write better Apex code and build more efficient solutions.
You may like to read:
- Query Salesforce Picklist Field values using SOQL
- Aggregate Query in Salesforce SOQL
- SOQL vs SQL Difference in Salesforce
- GROUP BY Date Clause in SOQL (Salesforce Object Query Language)
- GROUP BY Clause in SOQL (Salesforce Object Query Language)
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.