Section 1 · Lesson 2

Filtering rows with WHERE

Keep only the rows that answer your question, using comparisons, AND/OR, IN, BETWEEN and LIKE.

25 min 3 practice tasks
SQL Basics Tutorial For Beginners | Where Statement | 3/4 by Alex The Analyst on YouTube

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 says search.

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, IN and LIKE works 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.

1

Big-budget search campaigns

The 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?
  1. Both conditions must be true, so connect them with AND.
  2. "At least 1000" includes 1000 itself: use >=, not >.
SQL · SQLite
SELECT name, daily_budget
FROM campaigns
WHERE channel = 'search';
Tables in this database
advertisers (12 rows)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
2

January sign-ups in Canada and the UK

Find 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?
  1. country IN ('CA', 'UK') is a shorter way to write two OR conditions.
  2. BETWEEN low AND high includes both ends. Dates are text in YYYY-MM-DD format, so wrap them in single quotes.
SQL · SQLite
SELECT first_name, last_name, country, signup_date
FROM customers
WHERE country = 'CA';
Tables in this database
advertisers (12 rows)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
3

Fix the AND/OR bug

Campaign 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?
  1. AND is evaluated before OR, so the starter really means: (Stellar AND active) OR (any paused campaign).
  2. Wrap the two status checks in parentheses, or use status IN ('active', 'paused').
SQL · SQLite
SELECT id, name, status
FROM campaigns
WHERE name LIKE 'Stellar%' AND status = 'active' OR status = 'paused';
Tables in this database
advertisers (12 rows)
id, name, industry, country
campaigns (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
daily_stats (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
customers (300 rows)
id, first_name, last_name, email, country, segment, age_group, signup_date
orders (900 rows)
id, customer_id, campaign_id, order_date, amount, status
ab_test (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue