6

Parent to Child – Relationship Queries in SOQL

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:

  1. Bank (parent) → Branch (child)
  2. Project (Parent) → Task (Child)
  3. 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)

Start Your Salesforce Developer Journey CTA
Next Topic

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.

Frequently Asked Questions

What is a parent-to-child relationship in SOQL?

Parent-to-child relationship query in SOQL is a subquery that gets data from the parent object and related child objects in a single query.

How do I write a subquery in SOQL for custom objects?

Use the child relationship name with __r instead of the object standard API name to write a subquery for Custom objects in SOQL.

What is the child relationship name in Salesforce?

A child relationship name in Salesforce is used to uniquely identify a child record while traveling from a parent record to its child record in SOQL queries.

How many subqueries can you have in an SOQL query?

A maximum of 20 parent-to-child relationship subqueries can be in a Single SOQL query in Salesforce.

Book Free15-Minutes Career Counselling