Sub topic 8
Querying related records
This chapter assumes you can write a basic inline SOQL query with a WHERE clause and that you know lookup and master-detail relationships. SOQL does not use SQL joins. It follows relationships that already exist between objects, in two directions: child to parent and parent to child.
Child to parent: dot notation
A Contact has a lookup to Account. From a Contact query, you reach Account fields with dot notation on the relationship name. For standard objects the relationship name is the lookup field name without Id, so AccountId becomes Account. You can use parent fields in both SELECT and WHERE.
List<Contact> contacts = [
SELECT Id, LastName, Account.Name
FROM Contact
WHERE Account.Industry = 'Technology'
];
for (Contact c : contacts) {
System.debug(c.LastName + ' works at ' + c.Account.Name);
}Each Contact holds its parent Account as a nested sObject. If a Contact has no Account, c.Account is null, so reading c.Account.Name throws a NullPointerException. Check for null first when the lookup is optional.
For custom lookup fields, the relationship name ends in __r. For example, Project__c becomes Project__r. You can traverse up to five levels, such as Contact.Account.Owner.Manager.Name.
Parent to child: subqueries
To get an Account with its Contacts, put a subquery in the SELECT list. The subquery uses the plural child relationship name, Contacts, in the FROM clause.
List<Account> accts = [
SELECT Name, (SELECT LastName, Email FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
];
for (Account a : accts) {
for (Contact c : a.Contacts) {
System.debug(a.Name + ': ' + c.LastName);
}
}Each Account carries a list of its child Contacts in a.Contacts. If an Account has no Contacts, the list is empty and not null, so the inner loop is safe.
How the two directions compare
| Direction | Syntax | Result shape |
|---|---|---|
| Child to parent | `Account.Name` | Nested sObject on each child record |
| Parent to child | `(SELECT ... FROM Contacts)` | List of child records on each parent |
Common errors
- Wrong relationship name. Writing
FROM Contactinside the subquery gives an error. Use the plural relationship name,Contacts. - Null parent. Dot notation returns null when the lookup is empty. Guard the access.
- Unqueried fields. Reading
c.Account.Industrywithout selectingAccount.Industrythrows aSystem.SObjectException. Only fields you select are available. - Counting queries. A query with subqueries counts as one SOQL query. The child rows still count toward the total rows retrieved.
Exercise
You run SELECT Name, (SELECT LastName FROM Contacts) FROM Account. An Account in the results has no Contacts. What does a.Contacts contain, and is it safe to loop over it?
Show answer
It is an empty list, not null, so looping over it is safe and runs zero times.
Exercise
Write a query that returns each Opportunity's Name and the Name of its Account, but only for Opportunities whose Account has Industry 'Energy'.
Show answer
SELECT Name, Account.Name FROM Opportunity WHERE Account.Industry = 'Energy'