Sub topic 12
Combining Both Directions in One Query
This chapter assumes you can write a child-to-parent query with dot notation and a parent-to-child subquery, and that you know the difference between a relationship name and a field name. Here you will put both directions into a single statement and learn where the platform limits that.
Why combine directions
Reports and integrations often need a parent, its children, and some context about each. Without a combined query, you would run one query per direction and join the results in Apex. One statement is shorter and uses fewer queries against your governor limits.
Two rules make this work:
- Dot notation can appear in the outer SELECT list, in the subquery's SELECT list, and in WHERE and ORDER BY clauses.
- A subquery is a separate query scope. Its WHERE, ORDER BY, and LIMIT apply only to the child rows.
A worked example
This query starts from Account. It reads the account's owner (child-to-parent from the account) and the contacts (parent-to-child). For each contact, it reads the user who created the record (child-to-parent from the contact).
SELECT Id, Name, Owner.Name,
(SELECT Id, LastName, CreatedBy.Name
FROM Contacts
WHERE Email != null
ORDER BY LastName
LIMIT 5)
FROM Account
WHERE Industry = 'Technology'Each direction uses its own name:
| Part of the query | Direction | Name used |
|---|---|---|
| `Owner.Name` | Account to its owner | Parent relationship name |
| `FROM Contacts` | Account to its contacts | Child relationship name (plural) |
| `CreatedBy.Name` | Contact to its creator | Parent relationship name |
The LIMIT 5 applies to each account, so every account returns up to five contacts.
The same pattern on custom objects
For custom objects the names carry the __r suffix. The outer query reads the project's owner, and the subquery reads the tasks and each task's own parent fields.
SELECT Id, Name, Owner.Name,
(SELECT Id, Name, CreatedBy.Name FROM Tasks__r)
FROM Project__c
WHERE Status__c = 'Active'Reading the result in Apex
The result follows the same shape as the query. Parent fields come from the parent sObject, and children come from a list.
for (Account a : [
SELECT Id, Name, Owner.Name,
(SELECT LastName, CreatedBy.Name FROM Contacts LIMIT 5)
FROM Account
WHERE Industry = 'Technology'
LIMIT 10
]) {
System.debug(a.Name + ' is owned by ' + a.Owner?.Name);
for (Contact c : a.Contacts) {
System.debug(c.LastName + ' created by ' + c.CreatedBy?.Name);
}
}An account with no matching contacts returns an empty list, not null, so the loop is safe. A lookup field that is empty returns a null parent, so use ?. for parents that can be missing.
Limits to design around
A combined query counts against several relationship limits:
- A query can traverse up to 5 levels of parent relationships in dot notation.
- A query can include up to 55 child-to-parent relationships.
- A query can include up to 20 parent-to-child relationships, which means 20 subqueries.
- Subqueries can be nested up to 5 levels deep in recent API versions. Check the limits for your org's API version.
Common mistakes
- Using the parent relationship name for a subquery.
FROM Accountinside a subquery on Contact fails. The subquery's FROM must be a child relationship name, such asContacts. - Forgetting
__ron custom relationships. The field isProject__c, but the path isProject__r.Name. - Expecting the subquery's LIMIT to cap the total. It caps children per parent.
- Selecting wide subqueries on many parents. Large child lists make the result heavy, so filter inside the subquery.
Exercise
Write a SOQL query on Project_c that returns each project's Id and Name, the name of its owner, and its tasks (Id, Name, and the task creator's name), limited to the 3 newest tasks per project. Tasks are in the child relationship Tasks_r.
Show answer
SELECT Id, Name, Owner.Name, (SELECT Id, Name, CreatedBy.Name FROM Tasks_r ORDER BY CreatedDate DESC LIMIT 3) FROM Project_c