Parent to Child – Relationship Queries in SOQL
Chapter Topics
- SOQL Basics (Salesforce Object Query Language)
- How to Write SOQL in APEX
- SOQL Variable Binding in APEX
- SOQL Keywords
- SOQL Date Literals In Salesforce
- SOQL Aggregate Query
- Child to Parent Relationship
- Parent to Child – Relationship Queries in SOQL
- SOQL Multi level Relationships
- SOQL Return Type
- Salesforce Dynamic SOQL
- SOQL ’for’ Loops
Understanding relationships between different objects is important for managing Salesforce data effectively. One of the most commonly used query patterns in Salesforce is the Parent-to-Child relationship query, which enables you to organize and structure data in a way that reflects real-world scenarios. This query pattern helps in building robust data models that support complex business processes.
A parent-to-child relationship in SOQL retrieves parent records with their associated child records within a single query. In this guide, we will look at how you can use SOQL inner query in Salesforce to fetch data from standard as well as custom objects.
What is a Parent-to-Child Relationship Query?
A Parent-to-Child relationship in Salesforce query allows you to retrieve data from related objects where a parent object is related to multiple child records through lookup or master-detail relationships. It means that for each record in the parent object, there can be multiple related records in the child object.
Parent-to-child queries retrieve parent records along with their related child records using a single subquery in SOQL Salesforce, making it convenient to access related records. A parent-to-child query can return up to 50,000 parent records, and a maximum of 20 parent-to-child relationship subqueries can be in a Single SOQL query in Salesforce.
Example:
In the case of an Account object (parent) and a Contact object (child), the Account can have multiple associated Contacts. Using a Parent-to-Child relationship in SOQL, you can retrieve an Account along with its related Contacts (subject to SOQL limits) in a single query.
For Standard Objects
Salesforce provides predefined parent to child SOQL standard objects. Such as:
- Account (Parent) → Contact (Child)
- Opportunity (Parent) → Opportunity Product (Child)
- Case (Parent) → Case Comment (Child)
Example SOQL relationship queries in Salesforce:
List AccountData = [SELECT Id, Name, Industry, AnnualRevenue, NumberOfEmployees, (SELECT FirstName, LastName FROM Contacts) FROM Account];
List OpportunityData = [SELECT Id, Name, StageName, Amount, CloseDate, (SELECT Id, Quantity, UnitPrice FROM OpportunityLineItems) FROM Opportunity];
List CaseData = [SELECT Id, ParentId, Subject, Description, Priority, (SELECT Id, IsPublished, CommentBody FROM CaseComments) FROM Case];
Here, CaseComments is the child relationship name used in the subquery.
Note: The field name “IsPublished” is relevant in the portal/community contexts.
For Custom Objects
For custom objects, you must use the child relationship name, ending with __r, but it is defined in the object schema and may differ from the object name.
Example of parent to child SOQL custom object:
- Bank (parent) → Branch (child)
- Project (Parent) → Task (Child)
- Department (Parent) → Employee (Child)
SELECT Bank_Name__c,
(SELECT Branch_Id__c, Name, State__c FROM Branches__r)
FROM Bank__c
This custom query will fetch:
- The Bank_Name__c field from the Bank__c object (parent)
- A subquery that selects Branch_Id__c, Name, and State__c fields from the Branches__r object (child) that are related to each Bank__c record
- The __r suffix is used for the child relationship name for custom objects.
*Branches__r( is a child relationship name, not object API name)

Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.