Sub topic 9
Filtering parents by their children: semi-joins and anti-joins
This chapter assumes you can write basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT). It also assumes you know how lookup and master-detail relationships link objects, and that you have seen child-to-parent and parent-to-child queries from the earlier chapters.
The problem: parents with or without children
A parent-to-child subquery in the SELECT list returns every parent and attaches its children. It does not remove parents that have no children. Those parents come back with an empty child list.
Sometimes you want to filter the parents themselves. For example, you may want only the accounts that have at least one contact with an email address. Or you may want only the accounts that have no opportunities at all. SOQL handles this with a subquery in the WHERE clause, joined by IN or NOT IN.
- A semi-join is a WHERE clause that uses
INwith a subquery. It keeps the parents that match at least one row in the subquery. - An anti-join is a WHERE clause that uses
NOT INwith a subquery. It keeps the parents that match no row in the subquery.
Semi-join: parents that have matching children
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact WHERE Email != null)The inner query selects the AccountId of every contact that has an email. The outer query keeps each account whose Id is in that list. An account with five matching contacts still appears once.
The inner query selects a lookup field (AccountId), not a relationship name. A subquery in the WHERE clause must return a single field. The field's type must match the type of the field on the left of IN. Here both are IDs.
Anti-join: parents that have no matching children
SELECT Id, Name
FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Opportunity)This returns the accounts that no opportunity points to. The query is useful for finding gaps, such as customers who have never bought anything.
Two directions, two tools
The two techniques answer different questions. The table compares them.
| Goal | Technique | Where the subquery goes |
|---|---|---|
| Return parents and show their children | Parent-to-child subquery | SELECT list |
| Return only parents that have matching children | Semi-join | WHERE clause with `IN` |
| Return only parents that have no matching children | Anti-join | WHERE clause with `NOT IN` |
A semi-join does not return child data. If you need both the filter and the child fields, put a subquery in the SELECT list and a semi-join in the WHERE clause of the same query.
SELECT Id, Name,
(SELECT Id, LastName FROM Contacts WHERE Email != null)
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact WHERE Email != null)How the query is evaluated
Common mistakes
- Selecting the wrong inner field.
WHERE Id IN (SELECT Id FROM Contact)compares an account ID with contact IDs and returns nothing. Select the lookup field that points back to the parent. - Selecting more than one field in the inner query. The inner query of a semi-join or anti-join must return exactly one column.
- Expecting child data. The WHERE subquery filters only. Add a SELECT-list subquery to read the children.
- Forgetting the limits. Each subquery counts toward the query limits covered in the final chapter of this tutorial.
Exercise
Write a query that returns the Id and Name of every Account that has at least one Opportunity, and explain why the account appears only once even if it has many opportunities.
Show answer
SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity). The inner query produces a set of AccountId values. The outer query tests each account's Id against that set, so each account is returned once, no matter how many opportunities point to it.
Exercise
Which operator and subquery shape would you use to find Accounts that have no Contact with an email address?
Show answer
Use an anti-join: SELECT Id, Name FROM Account WHERE Id NOT IN (SELECT AccountId FROM Contact WHERE Email != null). The inner query selects the single lookup field AccountId, and NOT IN keeps accounts that are absent from that set.