Sub topic 4
Filtering and sorting on parent fields
This chapter assumes you can write basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT). It also assumes you know how to pick a relationship name, such as Account on Contact or Project__r on a custom lookup. Here you will use those names outside the SELECT list.
Parent fields work in WHERE and ORDER BY
Dot notation is not limited to the SELECT list. You can use the same path in WHERE and ORDER BY to filter and sort child records by values on their parent.
SELECT Id, LastName, Account.Name, Account.Owner.Name
FROM Contact
WHERE Account.Industry = 'Technology'
ORDER BY Account.NameThis query returns contacts whose account is in the Technology industry, sorted by account name. The filter and the sort field do not have to appear in the SELECT list. Here Account.Name is both selected and sorted, but the query would still be valid without it in the list.
SELECT Id, LastName
FROM Contact
WHERE Account.Industry = 'Technology'
ORDER BY Account.NameCustom objects
For a custom lookup or master-detail field, use the relationship name that ends in __r, not the field name that ends in __c.
SELECT Id, Name, Project__r.Name
FROM Task__c
WHERE Project__r.Status__c = 'Active'
ORDER BY Project__r.Name, NameProject__c holds the ID of the project. Project__r is the path to the project record, so Project__r.Status__c reads a field on it. Writing Project__c.Status__c is a common mistake and fails with an invalid field error.
What happens when the parent is missing
A lookup field can be empty. When a child has no parent, every parent field on it evaluates to null. This has three effects:
- A filter such as
Account.Industry = 'Technology'is false for contacts with no account, so they are excluded. - A filter such as
Account.Industry != 'Technology'also excludes them. Comparisons withnulldo not match, so contacts with no account will not appear here. - In sorting, records with no parent have a
nullsort value. Ascending order puts nulls first by default.
You can control where nulls go with NULLS FIRST or NULLS LAST.
SELECT Id, LastName, Account.Name
FROM Contact
ORDER BY Account.Name ASC NULLS LASTTo find contacts that have no account, test the lookup field itself instead of a parent field.
SELECT Id, LastName
FROM Contact
WHERE AccountId = nullAccount.Name = null is less precise. It matches contacts with no account, but it would also match a contact whose account exists and has a blank name.
Prefer the ID field when you only need the ID
If you already know the parent's ID, filter on the lookup field. It is shorter and does not need to follow the relationship.
SELECT Id, LastName
FROM Contact
WHERE AccountId = '001000000000001AAA'Use Account.Id only when you are building a longer path, such as Account.Owner.Id.
Several parent conditions together
You can combine parent filters with child filters and with each other using AND and OR. You can sort by more than one field, mixing parent and child fields.
SELECT Id, LastName, Account.Name, Account.Industry
FROM Contact
WHERE Account.Industry IN ('Technology', 'Finance')
AND Email != null
ORDER BY Account.Name ASC, LastName DESC
LIMIT 50Sorting happens before LIMIT is applied, so you get the first 50 contacts in that order.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No such column 'Project__c.Status__c'` | Field name used where a relationship name is needed | Use `Project__r.Status__c` |
| Contacts with no account are missing | The parent filter is false for null parents | Add `OR AccountId = null` if you want them |
| Unexpected order for records without a parent | Nulls sort first in ascending order | Add `NULLS LAST` |
Exercise
Write a query that returns the Id and Name of Task_c records whose project has Status_c equal to 'Active', sorted by project name with records that have no project listed last. Which part of the query would you change to also include tasks that have no project?
Show answer
SELECT Id, Name FROM Task_c WHERE Projectr.Statusc = 'Active' ORDER BY Projectr.Name NULLS LAST. A task with no project fails the Projectr.Statusc filter, so to include those tasks you would change the WHERE clause to (Projectr.Statusc = 'Active' OR Project_c = null).