Section 1 · Lesson 2
Filtering rows with WHERE
Keep only the rows that answer your question, using comparisons, AND/OR, IN, BETWEEN and LIKE.
Most business questions are about some of the data, not all of it: “which search campaigns
are still running?”, “who signed up last month?”, “which orders were refunded?”. The WHERE
clause keeps only the rows for which a condition is true and drops the rest.
SELECT name, daily_budget
FROM campaigns
WHERE status = 'active';
WHERE always comes after FROM. The database reads the table, tests every row against the
condition, and returns only the rows that pass.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= |
equal to | channel = 'video' |
<> or != |
not equal to | status <> 'ended' |
> / >= |
greater than / or equal | daily_budget >= 1000 |
< / <= |
less than / or equal | amount < 20 |
Text values go in single quotes ('search'); numbers don’t. In this dataset dates are
stored as text in YYYY-MM-DD format. That format sorts the same way as the calendar, so
start_date >= '2026-02-01' correctly finds campaigns that started in February or later.
Watch the case: in SQLite,
=on text is case-sensitive.channel = 'Search'finds nothing because the data sayssearch.
Combining conditions: AND, OR, NOT
AND: both conditions must be true.channel = 'video' AND status = 'active'OR: at least one must be true.status = 'active' OR status = 'paused'NOT: flips a condition.NOT channel = 'display'
When you mix them, AND is evaluated before OR, just like multiplication before addition
in math. This is one of the most common bugs in real reports:
-- Intended: video campaigns that are active or paused
WHERE channel = 'video' AND status = 'active' OR status = 'paused'
-- What SQL reads: (video AND active) OR (paused, on ANY channel)
WHERE channel = 'video' AND (status = 'active' OR status = 'paused') -- correct
When in doubt, add parentheses. They cost nothing and make your intent obvious to the next reader.
IN: a cleaner list of ORs
SELECT first_name, last_name, country
FROM customers
WHERE country IN ('CA', 'UK', 'DE');
NOT IN (...) does the opposite. IN is easier to read and harder to get wrong than a chain of
ORs.
BETWEEN: ranges, both ends included
SELECT id, order_date, amount
FROM orders
WHERE amount BETWEEN 50 AND 100; -- same as amount >= 50 AND amount <= 100
It works for dates too: order_date BETWEEN '2026-03-01' AND '2026-03-31'. Be careful if a
column ever holds date and time (2026-03-31 14:05): that value is “greater” than
'2026-03-31' and would be left out. Our dates are plain dates, so BETWEEN is safe here.
LIKE: matching patterns in text
| Pattern | Matches |
|---|---|
'Urban%' |
starts with “Urban” |
'%Video%' |
contains “Video” anywhere |
'%38' |
ends with “38” |
'_ora' |
any one character followed by “ora” (Nora, Dora) |
% stands for any run of characters (including none) and _ for exactly one. In SQLite,
LIKE ignores upper/lower case for English letters, unlike PostgreSQL where you’d need ILIKE.
Counting matches
While exploring, you’ll often want to know how many rows match rather than see them all.
Put COUNT(*) in the SELECT:
SELECT COUNT(*) FROM campaigns WHERE channel = 'video';
You’ll learn COUNT and its relatives properly in the next section, but this pattern is handy
right away.
About the video: it uses SQL Server, but everything shown about
WHERE,AND/OR,INandLIKEworks the same way in SQLite.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Big-budget search campaigns
DoneThe paid-search team wants to review its biggest campaigns. Return the name and daily_budget (in that order) of every campaign on the search channel with a daily budget of at least 1000 USD.
Need a hint?
- Both conditions must be true, so connect them with
AND. - "At least 1000" includes 1000 itself: use
>=, not>.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT name, daily_budget
FROM campaigns
WHERE channel = 'search';
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)January sign-ups in Canada and the UK
DoneFind customers from Canada (CA) or the United Kingdom (UK) who signed up in January 2026 (from 2026-01-01 through 2026-01-31, both days included). Return first_name, last_name, country and signup_date, in that order.
Need a hint?
country IN ('CA', 'UK')is a shorter way to write twoORconditions.BETWEEN low AND highincludes both ends. Dates are text inYYYY-MM-DDformat, so wrap them in single quotes.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT first_name, last_name, country, signup_date
FROM customers
WHERE country = 'CA';
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)Fix the AND/OR bug
DoneCampaign names start with the advertiser's brand. A colleague wants every Stellar campaign (name starts with Stellar) that is still live, meaning its status is active or paused. Their query (the starter) returns campaigns from other brands too. Fix it so it returns only the right rows, with columns id, name and status.
Need a hint?
ANDis evaluated beforeOR, so the starter really means: (Stellar AND active) OR (any paused campaign).- Wrap the two status checks in parentheses, or use
status IN ('active', 'paused').
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
SELECT id, name, status
FROM campaigns
WHERE name LIKE 'Stellar%' AND status = 'active' OR status = 'paused';
Tables in this database
advertisers (12 rows)campaigns (40 rows)daily_stats (1,561 rows)customers (300 rows)orders (900 rows)ab_test (4,000 rows)