Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

The most recent version of SOQL allows relationship queries to traverse up to five levels of parent-child data. This implies that a single SOQL query can be used to retrieve parent-child records from five different levels.

Please be aware, nevertheless, that this capability is limited to doing SOQL queries on both standard and custom objects through REST and SOAP query calls.
The maximum number of Parent-to-Child connection levels for SOQL searches in Salesforce’s Apex class is still limited to two.

Relationship queries in SOQL can have a parent root as the first level and go up to four levels deep with child relationships starting with API version 58.0. With this update, developers may now create complex searches that retrieve data from several parent and child items simultaneously. Before this improvement, these kinds of inquiries were to be made as several independent requests, which added to the complexity and might have affected performance.

Watch The Video Or Read The Article (Or Do Both smile)

Data Model

I have created a data model with five level deep by introducing three new custom objects as Third Object, Fourth Object & Fifth Object. Third Object has a lookup relationship with Contact, Fourth Object has a lookup relationship with Third Object & Fifth Object has a lookup relationship with Fourth Object.

Below is the screenshot of the data model.

Let’s Write The SOQL Query

I have manually loaded some data into Third, Fourth and Fifth custom object manually. With the data set, the query to fetch all five level data in single SOQL query will look like –

SELECT Id, Name,
 SELECT ID, LastName,
 SELECT ID FROM Fifth_Objects__r
 FROM Fourth_Objects__r
 FROM Third_Objects__r
 FROM Contacts
 FROM Account

When I executed the above SOQL query in workbench, here comes the result which fetches data till five level deep. 

Perform Callout

The query to execute the above query in REST API is


And the result came back with five level deep object data fetched.

Best Practices

Although performing five-level parent-to-child relationship queries is incredibly powerful and convenient, there are a few best practices and things to keep in mind:

1. API Version Compatibility: Keep in mind that starting with API version 58.0, you can use the enhanced parent-to-child relationship queries. To take advantage of this feature, you’ll need to upgrade your organization if it still uses an outdated version of the API.

2. Data Volume: Take into consideration the amount of data that these queries require. The amount of records that are obtained when navigating through several layers may be substantial, which may affect the query execution time. Examine your data model and make the necessary adjustments to the query depth.

3. Query Optimization: Performance optimization is crucial for any query. To reduce the volume of data returned, carefully review the fields you retrieve and apply selective filters. This method lowers the chance of exceeding governor limits while ensuring effective execution.

4. Query Complexity: Although the enhanced features make retrieving complicated data easier, the resulting queries may become more complex. Make sure your query logic is still comprehensible and manageable. If necessary, think about dividing up complicated searches into smaller, easier-to-manage chunks.

Final Thought

With REST and SOAP calls, parent-child data up to five layers deep can be efficiently retrieved in a single query according to the most recent SOQL upgrade. Nevertheless, the Apex class still has a two-level Parent-to-Child connection constraint, therefore this feature is limited to those calls.


This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.


Submit a Comment

Your email address will not be published. Required fields are marked *