Curriculum
Parent to Child Queries are a specialized type of SOQL Relationship Query that allow Salesforce developers to retrieve child records from a parent object in a single query. These queries are extremely useful when working with related data such as Accounts and Contacts, Accounts and Opportunities, Courses and Students, Customers and Orders, or Projects and Tasks.
Instead of querying parent records and child records separately, Parent to Child Queries use a nested query structure to retrieve all related information at once. This improves performance, reduces SOQL usage, and helps developers build scalable Salesforce applications.
Understanding Parent to Child Queries is essential for Salesforce Developers because they are commonly used in Apex Classes, Triggers, Lightning Components, Reports, Dashboards, and Integrations.
A Parent to Child Query retrieves child records associated with a parent record.
Example:
Account
├── Contact 1
├── Contact 2
└── Contact 3
The query starts from the parent object and retrieves related child records.
Organizations often need to display:
Parent to Child Queries make this possible using a single SOQL query.
Reduce Governor Limit usage.
Retrieve related records efficiently.
Reduce query complexity.
Support large applications.
View parent and child data together.
These benefits make Parent to Child Queries highly valuable.
The primary object.
The related object.
Example:
Account → Parent
Contact → Child
One Account can have many Contacts.
Parent:
Course
Children:
Student 1
Student 2
Student 3
The Course object is the parent.
Student records are children.
Parent to Child Queries use:
Nested SELECT Statements
General Syntax:
SELECT ParentFields,
(
SELECT ChildFields
FROM ChildRelationshipName
)
FROM ParentObject
The nested SELECT retrieves child records.
Parent to Child Queries use:
Child Relationship Name
instead of the child object name.
Example:
Parent:
Account
Child:
Contact
Relationship Name:
Contacts
This relationship name is used in the query.
Example:
SELECT Name,
(
SELECT FirstName,
LastName
FROM Contacts
)
FROM Account
This retrieves:
Both are returned together.
Account:
Groot Software
Contacts:
Rahul Sharma
Priya Singh
Amit Kumar
The query returns the parent record and all related child records.
Parent Fields:
SELECT Name
Child Query:
(
SELECT FirstName,
LastName
FROM Contacts
)
Parent Object:
FROM Account
These components create a complete Parent to Child Query.
Example:
SELECT Name,
(
SELECT Name,
Amount
FROM Opportunities
)
FROM Account
Returns:
This is a common business scenario.
Account:
ABC Technologies
Opportunities:
| Opportunity | Amount |
|---|---|
| Software Project | ₹500000 |
| Mobile App Project | ₹300000 |
All related opportunities are retrieved.
Example:
SELECT Name,
(
SELECT CaseNumber,
Subject
FROM Cases
)
FROM Account
Returns Accounts and their Cases.
Useful for support management.
Custom relationships also support Parent to Child Queries.
Parent:
Course__c
Child:
Student__c
Relationship Name:
Students__r
Custom relationships typically use:
__r
notation.
SELECT Name,
(
SELECT Name,
Email__c
FROM Students__r
)
FROM Course__c
Returns Courses and enrolled Students.
Course:
Salesforce Development
Students:
| Student | |
|---|---|
| Rahul Sharma | rahul@email.com |
| Priya Singh | priya@email.com |
All related students are returned.
Object Manager
Schema Builder
Workbench
Developer Console
Developers should verify relationship names before writing queries.
Filtering can be applied to parent records.
Example:
SELECT Name,
(
SELECT FirstName,
LastName
FROM Contacts
)
FROM Account
WHERE Industry = 'Software'
Only Software Accounts are returned.
Example:
SELECT Name,
(
SELECT FirstName
FROM Contacts
)
FROM Account
LIMIT 5
Returns only five parent records.
LIMIT improves performance.
Example:
SELECT Name,
(
SELECT FirstName,
LastName
FROM Contacts
ORDER BY LastName
)
FROM Account
Child records are sorted alphabetically.
Example:
SELECT Name,
(
SELECT Name,
Amount
FROM Opportunities
WHERE Amount > 100000
)
FROM Account
Only qualifying Opportunities are returned.
This improves data relevance.
Example:
List<Account> accounts =
[
SELECT Name,
(
SELECT FirstName,
LastName
FROM Contacts
)
FROM Account
];
The result contains parent and child data.
Example:
for(Account acc : accounts){
System.debug(
acc.Name
);
for(Contact con :
acc.Contacts){
System.debug(
con.FirstName
);
}
}
Both parent and child records can be processed.
Salesforce imposes limits on:
Developers should design queries carefully.
A software training company wants course details with enrolled students.
Query:
SELECT Name,
(
SELECT Name,
Email__c
FROM Students__r
)
FROM Course__c
Result:
Course:
Salesforce Development
Students:
Management receives all information in one query.
Improve performance.
Avoid errors.
Reduce processing.
Improve relevance.
Verify results.
These practices support efficient Salesforce development.
Developers should avoid these mistakes.
Understanding Parent to Child Queries helps professionals:
These queries are widely used in real-world Salesforce projects.
Parent to Child Queries allow Salesforce developers to retrieve child records from a parent object using nested SELECT statements. By leveraging relationship names, subqueries, filters, sorting, and Apex integration, developers can efficiently access related Salesforce data. Mastering Parent to Child Queries is essential for building scalable, high-performance Salesforce applications and reducing query complexity.
A Parent to Child Query retrieves related child records from a parent object.
Nested SELECT statements are used.
A Child Relationship Name identifies related child records in a Parent to Child Query.
Yes. Custom objects support Parent to Child Queries using relationship names ending in __r.
Yes. WHERE clauses can be used inside child subqueries.
They improve performance, reduce SOQL usage, and simplify data retrieval.
Looking to learn more technologies and programming skills?
WhatsApp us