Parent-to-Child and Child-to-Parent Queries
By the end of this lesson, you'll be able to:
- Write a child-to-parent query using dot notation
- Write a parent-to-child query using a nested subquery
Prerequisites: None — this is the first lesson in the course.
Child-to-parent: dot notation
A child record has exactly one parent, so you can traverse the relationship with simple dot notation:
SELECT Id, Account.Name FROM Contact
Dot notation can chain across more than one level — Account.Owner.Name walks from a Contact up to its Account and then to that Account's owning User.
Parent-to-child: nested subqueries
A parent can have many children, so a single dotted field wouldn't make sense — instead, a nested subquery inside parentheses returns the related list:
SELECT Id, (SELECT Id, LastName FROM Contacts) FROM Account
The subquery uses the plural relationship name (Contacts) for standard relationships, or a custom relationship name ending in __r for custom objects.
A parent query with a nested child subquery
SELECT Id, Name, (SELECT Id, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
The nested SELECT returns each matching Account's related Contacts as a child list — accessible in Apex via account.Contacts after the query runs.
Exercise
Write a SOQL query that returns each Opportunity's Id and Name, along with its parent Account's Name.
Show hint
Use dot notation: Account.Name
Parent-to-Child and Child-to-Parent Queries — Quick Check
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Child-to-parent queries traverse a relationship with dot notation since a child has exactly one parent; parent-to-child queries use a nested subquery since a parent can have many children.