Intermediate
25 min read
Filtering with WHERE
By the end of this lesson, you'll be able to:
- Filter query results using a WHERE clause
- Combine multiple conditions with AND and OR
- Use LIKE for partial text matching
Prerequisites: "Your First SOQL Query"
A basic WHERE clause
List<Account> techAccounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];
List<Account> techAccounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];— this is exactly the comparison operators from Module 4, now filtering which records the database returns rather than branching Apex code —=,!=,>,<,>=,<=all work the same way inside aWHEREclause.
Combining conditions with AND / OR
List<Account> largeTechAccounts = [
SELECT Id, Name FROM Account
WHERE Industry = 'Technology' AND NumberOfEmployees > 500
];
List<Account> techOrFinance = [
SELECT Id, Name FROM Account
WHERE Industry = 'Technology' OR Industry = 'Finance'
];
List<Account> largeTechAccounts = [— a query with two combined conditions.SELECT Id, Name FROM Account— the fields and object.WHERE Industry = 'Technology' AND NumberOfEmployees > 500—ANDrequires every condition to be true.];— closes the query.- (blank line)
List<Account> techOrFinance = [— a second query.SELECT Id, Name FROM Account— the fields and object.WHERE Industry = 'Technology' OR Industry = 'Finance'—ORrequires just one — both work exactly like Module 6's logical operators.];— closes the query.
Partial matching with LIKE
List<Account> matches = [SELECT Id, Name FROM Account WHERE Name LIKE 'Acme%'];
List<Account> matches = [SELECT Id, Name FROM Account WHERE Name LIKE 'Acme%'];—LIKEmatches partial text —%is a wildcard standing in for any sequence of characters, so'Acme%'matches "Acme Logistics," "Acme Corp," and anything else starting with "Acme." This is the SOQL equivalent of a "starts with" or "contains" check.
Exercise
Write a query selecting Id and Name from Contact where LastName starts with 'D' and MailingCity equals 'Cape Town'.
Show hint
WHERE LastName LIKE 'D%' AND MailingCity = 'Cape Town'
Filtering with WHERE Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
WHERE narrows a query down to only the records that matter — using comparison and logical operators that will feel very familiar from Modules 4 and 6.