Sub topic 6
Parent-to-child: subqueries in the SELECT list
This chapter assumes you can write basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT). It also assumes you know what lookup and master-detail relationships are, and that you know the difference between a relationship name and a field name from the previous chapters.
What a parent-to-child query does
A child-to-parent query starts at the child object and reads fields from one parent. A parent-to-child query goes the other way. It starts at the parent object and returns, for each parent record, a list of its related child records.
You write this as a subquery inside the SELECT list of the outer query. The subquery's FROM clause does not name the child object. It names the child relationship name, which is the plural name of the relationship as seen from the parent.
SELECT Id, Name,
(SELECT Id, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'Here Contacts is the child relationship name on Account. The query returns each matching Account, and each Account carries a list of its Contact records.
Finding the child relationship name
For standard objects, the child relationship name is usually the plural of the child object: Contacts, Opportunities, Cases. For custom objects, it is the relationship name defined on the lookup or master-detail field, with the __r suffix.
SELECT Id, Name,
(SELECT Id, Name FROM Tasks__r)
FROM Project__cIn this example, the field Project__c on Task__c has a child relationship name of Tasks. In queries it appears as Tasks__r. You can find the exact value in Setup, in the field's definition under "Child Relationship Name". The describe result for the parent object also lists it.
Using WHERE, ORDER BY, and LIMIT inside a subquery
The subquery is a full query on the child records. It accepts its own WHERE, ORDER BY, and LIMIT clauses. These apply only to the children. They do not filter the parent records.
SELECT Id, Name,
(SELECT Id, LastName FROM Contacts ORDER BY LastName LIMIT 5)
FROM Account
WHERE Industry = 'Technology'This query returns every Technology account. Each account has at most five contacts, sorted by last name. An account with no contacts is still returned, with an empty child list.
The outer WHERE clause filters parents. The inner WHERE clause filters children. Mixing them up is a common mistake.
Reading the results in Apex
In Apex, the child records come back as a list stored on the parent record. You read it through the child relationship name.
for (Account a : [SELECT Id, Name, (SELECT LastName FROM Contacts) FROM Account LIMIT 10]) {
System.debug(a.Name + ' has ' + a.Contacts.size() + ' contacts');
for (Contact c : a.Contacts) {
System.debug(c.LastName);
}
}When a parent has no children, a.Contacts is an empty list, not null, so size() and the inner loop are safe. This is different from a child-to-parent field such as c.Account, which can be null.
How the result is shaped
The query returns one row per parent. It does not return one row per child, as a SQL join would. The children are nested inside each parent.
| Query direction | Rows returned | Related data |
|---|---|---|
| Child-to-parent | One per child record | Parent fields on each row |
| Parent-to-child | One per parent record | List of child records on each row |
Common errors
- Using the child object name instead of the relationship name in the subquery's FROM clause.
- Forgetting
__ron custom relationship names, or adding it to a standard one such asContacts. - Expecting the subquery's WHERE clause to remove parents that have no matching children. It does not. To filter parents by their children, use a semi-join, covered in a later chapter.
- Selecting the child name field but not the fields you later read in Apex. Fields you do not select throw an error when accessed.
Exercise
You run SELECT Id, Name, (SELECT Id FROM Contacts WHERE Email != null) FROM Account. An account has three contacts, and none has an email address. Is that account in the results, and what does a.Contacts contain in Apex?
Show answer
The account is still returned, because the inner WHERE clause filters only the children. a.Contacts is an empty list, not null, so a.Contacts.size() returns 0.