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'];

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 a WHERE clause.

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'
];

AND and OR in SOQL work exactly like Module 6's logical operators — AND requires every condition to be true, OR requires just one.

Partial matching with LIKE

List<Account> matches = [SELECT Id, Name FROM Account WHERE Name LIKE 'Acme%'];

LIKE matches 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'

APEX

Filtering with WHERE Quiz

1. What does the % wildcard do inside a LIKE clause?

Log in to submit the quiz and save your score.

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.