Sub topic 3
Filtering records with WHERE
This chapter assumes you can write a basic inline query with SELECT and FROM and that you know Apex strings, lists, and loops. Here you will narrow a query to the records you need with a WHERE clause.
What WHERE does
A WHERE clause holds one or more conditions. The database evaluates the conditions and returns only the rows that match, so the filtering happens before the records reach your Apex code. Filtering in the query is almost always better than querying everything and filtering in a loop. It uses less heap memory and returns fewer rows.
List<Account> accts = [
SELECT Id, Name, AnnualRevenue
FROM Account
WHERE Industry = 'Technology' AND AnnualRevenue > 1000000
];
System.debug(accts.size() + ' accounts matched');Comparison operators
SOQL uses a single = for equality, not ==. String literals need single quotes.
| Operator | Meaning | Example |
|---|---|---|
| `=`, `!=` | Equal, not equal | `Industry = 'Energy'` |
| `<`, `<=`, `>`, `>=` | Numeric, date, and text ordering | `NumberOfEmployees >= 100` |
| `LIKE` | Pattern match with `%` and `_` | `Name LIKE 'Acme%'` |
| `IN`, `NOT IN` | Match any value in a list | `Industry IN ('Energy', 'Utilities')` |
% matches any number of characters. _ matches exactly one. String comparisons with = and LIKE ignore case, so 'acme' matches Acme.
Combining conditions
Join conditions with AND, OR, and NOT. When you mix AND and OR, SOQL requires parentheses so the grouping is explicit.
List<Account> accts = [
SELECT Id, Name, Industry
FROM Account
WHERE (Industry = 'Energy' OR Industry = 'Utilities')
AND NOT Name LIKE 'Test%'
];The IN operator is shorter when you compare one field against several values. This query is equivalent to the OR group above:
List<Account> accts = [
SELECT Id, Name
FROM Account
WHERE Industry IN ('Energy', 'Utilities')
];Testing for empty fields
Compare with the keyword null to find records where a field has no value. Do not quote it.
List<Contact> noEmail = [
SELECT Id, LastName FROM Contact WHERE Email = null
];
List<Contact> withEmail = [
SELECT Id, LastName FROM Contact WHERE Email != null
];Common errors
- Using
==. Apex uses==, but SOQL uses=. The query fails to compile. - Double quotes around strings. SOQL accepts only single quotes. Inside an Apex string you must escape them.
- Quoting
null.Email = 'null'looks for the text "null", not an empty field. - Leading wildcards.
LIKE '%corp'is legal, but it scans many more rows thanLIKE 'Acme%'. The performance chapter covers why. - Unparenthesized
ANDandOR. SOQL rejects the ambiguous form. Group conditions on purpose. - Comparing the wrong type. A number field compared with a quoted string, such as
AnnualRevenue > '1000', causes a compile error.
Hard-coded values are fine for learning, but real code usually filters on variables. The next chapter shows how to bind them safely.
Exercise
Write a query that returns the Id and LastName of every Contact whose LastName starts with "Sm" and whose Email is not empty.
Show answer
List<Contact> result = [SELECT Id, LastName FROM Contact WHERE LastName LIKE 'Sm%' AND Email != null];
Exercise
Why does WHERE Industry = 'Energy' OR Industry = 'Utilities' AND AnnualRevenue > 1000000 need to be rewritten, and how would you write it so the revenue filter applies to both industries?
Show answer
SOQL requires parentheses when AND and OR are mixed, so the query is rejected as written. Group the OR: WHERE (Industry = 'Energy' OR Industry = 'Utilities') AND AnnualRevenue > 1000000.