Mastering Parent-to-Child SOQL Queries in Salesforce

Ricky BenitezRicky Benitez
2 min read

When working with Salesforce data using SOQL (Salesforce Object Query Language), developers often must fetch related child records while querying a parent object. SOQL makes this possible through relationship queries, and understanding these is key to writing powerful, efficient data queries in Salesforce.

In this blog post, you’ll learn how to use parent-to-child SOQL queries with real-world examples and tips for identifying relationship names.

What Is a Parent-to-Child SOQL Query?

In Salesforce, when two objects are related via a lookup or master-detail relationship, you can query a parent object and fetch its child records in a nested SELECT statement.

✅ General Syntax:

soqlCopyEditSELECT ParentField1, ParentField2,
       (SELECT ChildField1, ChildField2 FROM ChildRelationshipName)
FROM ParentObject

Notice that the subquery uses a child relationship name, not the child object name directly!

Example 1: Accounts and Contacts

Let’s say you want to get all Accounts and their related Contacts.

soqlCopyEditSELECT Name, Industry,
       (SELECT FirstName, LastName, Email FROM Contacts)
FROM Account

Here:

  • Account Is the parent object

  • Contacts Is the child relationship name (not just the object name Contact!)

Example 2: Opportunities and OpportunityLineItems

soqlCopyEditSELECT Name, StageName,
       (SELECT Quantity, TotalPrice FROM OpportunityLineItems)
FROM Opportunity

This fetches each Opportunity with its related line items (products).


🔍 How to Find the Child Relationship Name?

The key to writing these queries is knowing the correct child relationship name.

Here are 3 ways to find it:

1. Salesforce Setup

  • Go to Object Manager

  • Select the parent object (e.g., Account)

  • Scroll to Child Relationships

2. Workbench

  • Visit workbench.developerforce.com

  • Go to Info > Standard & Custom Objects

  • Choose the parent object

  • Look under Child Relationships

3. Schema Builder

  • Open the Schema Builder from Salesforce Setup

  • Visually inspect relationships between objects


💡 Pro Tips

  • Always use the plural name of the child relationship in the subquery.

  • These queries can only go one level deep in subqueries.

  • You can’t use child-to-parent syntax (Parent__r.FieldName) inside a subquery.


🔚 Conclusion

Parent-to-child SOQL queries are powerful tools that let you retrieve structured data across object relationships with a single call. Once you understand the syntax and how to find child relationship names, you’ll write much more efficient and maintainable Salesforce code.

Have questions or want a deep dive into child-to-parent queries? Let me know in the comments!

0
Subscribe to my newsletter

Read articles from Ricky Benitez directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ricky Benitez
Ricky Benitez

Greetings! I'm delighted to welcome you to my digital corner. My name is Ricky Benitez, and I'm a seasoned Full Stack Java Technical Lead with more than a decade of enriching experience in the ever-evolving realm of software development.