Filtering with WHERE: Comparisons, AND, OR, IN, BETWEEN and LIKE
Filter rows with WHERE, combine conditions with AND and OR, avoid precedence bugs, and use IN, BETWEEN and LIKE to write clearer filters.
Welcome back!
Step 1 of 15Learn
Asking for specific rows
A query without a filter returns every row. Real questions are specific: Which books cost more than $20? Which customers live in Austin? WHERE keeps only the rows that satisfy a condition.
SELECT title, price
FROM books
WHERE price > 20;The database checks each row: is price greater than 20? Using the books table, 4 rows pass: Data in Practice (42.00), Learning to Cook (24.00), The Last Signal (21.00) and Quiet Numbers (35.00).
The comparison operators are:
| Operator | Meaning |
|---|---|
| = | equal to |
| <> or != | not equal to |
| > and < | greater than, less than |
| >= and <= | greater than or equal, less than or equal |
Text and dates are compared too. Text values go in single quotes:
SELECT title
FROM books
WHERE genre = 'Sci-Fi';Dates are usually written as 'YYYY-MM-DD' strings, and later dates count as greater: WHERE order_date >= '2026-02-01' keeps orders from February 1 onward.
WHERE goes right after FROM, and it filters before sorting or limiting:
SELECT title, price
FROM books
WHERE genre = 'Fiction'
ORDER BY price;Two things trip beginners up. First, SQL uses a single = for comparison, not == as in many programming languages. Second, whether text comparisons are case-sensitive depends on the database: 'sci-fi' may or may not match 'Sci-Fi'. Match the stored capitalization exactly, or use a function such as LOWER to be safe.
Step 2 of 15Note
Step 3 of 15Practice · Work it out
Using the books table, how many rows does this query return? SELECT title FROM books WHERE year < 2019;
Step 4 of 15Note
Step 5 of 15Learn
Combining conditions with AND, OR and NOT
AND requires both conditions to be true:
SELECT title
FROM books
WHERE genre = 'Tech' AND price < 40;Only Quiet Numbers (Tech, 35.00) qualifies; Data in Practice is Tech but costs 42.00.
OR requires at least one condition to be true:
SELECT title
FROM books
WHERE genre = 'Mystery' OR price > 30;This returns Night Ferry (Mystery), Data in Practice and Quiet Numbers (both over 30).
NOT reverses a condition: WHERE NOT genre = 'Fiction'.
Now the trap. AND is evaluated before OR, just as multiplication comes before addition. Suppose you want Sci-Fi or Fiction books that cost under $16:
-- Wrong
SELECT title, genre, price
FROM books
WHERE genre = 'Sci-Fi' OR genre = 'Fiction' AND price < 16;SQL reads this as Sci-Fi OR (Fiction AND under 16). It returns both Sci-Fi books at any price (18.00 and 21.00) plus the two cheap Fiction books. Add parentheses to say what you mean:
-- Right
SELECT title, genre, price
FROM books
WHERE (genre = 'Sci-Fi' OR genre = 'Fiction') AND price < 16;Now only Gardens of Salt (15.50) and River Road (14.00) qualify.
Rule of thumb: whenever a WHERE clause mixes AND and OR, use parentheses, even if you think precedence would work out. It makes your intent clear to the database and to the next person who reads the query.
Step 6 of 15Practice · Diagram
AND vs. OR on the books table
Set A: genre = 'Tech'. Set B: year >= 2020. AND returns only the overlap; OR returns everything inside either circle. Tap a region.
Tap the region returned by WHERE genre = 'Tech' AND year >= 2020.
Tap the shape that answers the question, then press Check.
- genre = 'Tech'
- year >= 2020
- genre = 'Tech' only
Tech, but published 2018, so it fails the year condition.
- Quiet Numbers
- genre = 'Tech' + year >= 2020 (overlap)
Tech and 2022: the only book both conditions accept.
- Data in Practice
- year >= 2020 only
Recent, but not Tech.
- Gardens of Salt
- Learning to Cook
- The Last Signal
Step 7 of 15Practice · Code challenge · SQL
Return the title and price of the books that cost more than 20.
The database for this exercise (tables and rows)
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author TEXT NOT NULL,
genre TEXT,
price REAL,
year INTEGER,
stock INTEGER
);
INSERT INTO books VALUES
(1, 'The Silent Orbit', 'Mara Quinn', 'Sci-Fi', 18.00, 2019, 12),
(2, 'Gardens of Salt', 'Leo Park', 'Fiction', 15.50, 2021, 0),
(3, 'Data in Practice', 'Ana Ruiz', 'Tech', 42.00, 2022, 7),
(4, 'Night Ferry', 'Leo Park', 'Mystery', 12.99, 2017, 25),
(5, 'Learning to Cook', 'Sam Odell', 'Food', 24.00, 2020, 3),
(6, 'The Last Signal', 'Mara Quinn', 'Sci-Fi', 21.00, 2023, 9),
(7, 'Quiet Numbers', 'Ana Ruiz', 'Tech', 35.00, 2018, NULL),
(8, 'River Road', 'Jo Banks', 'Fiction', 14.00, 2016, 18);- Result matches the expected table
Step 8 of 15Practice · Work it out
Using the books table, how many rows does this query return? SELECT title FROM books WHERE author = 'Leo Park' OR genre = 'Tech' AND price > 40;
Step 9 of 15Learn
IN, BETWEEN and LIKE
Three operators make common filters shorter and clearer.
IN matches any value in a list, replacing a chain of ORs:
SELECT title, author
FROM books
WHERE author IN ('Leo Park', 'Jo Banks');That returns Gardens of Salt, Night Ferry and River Road. NOT IN excludes the listed values.
BETWEEN matches a range, including both ends:
SELECT title, year
FROM books
WHERE year BETWEEN 2018 AND 2021;This returns 4 books: The Silent Orbit (2019), Gardens of Salt (2021), Learning to Cook (2020) and Quiet Numbers (2018). It is the same as year >= 2018 AND year <= 2021. Be careful with dates that include times: BETWEEN '2026-01-01' AND '2026-01-31' on a timestamp column misses most of January 31, because it stops at midnight. For time ranges, many analysts prefer >= '2026-01-01' AND < '2026-02-01'.
LIKE matches text patterns using two wildcards:
- % matches any sequence of characters, including none.
- _ matches exactly one character.
SELECT title
FROM books
WHERE title LIKE 'The%';This finds titles starting with "The": The Silent Orbit and The Last Signal. Other patterns:
'%Road'ends with "Road"'%ight%'contains "ight" anywhere (Night Ferry)'_ata%'has any one character, then "ata" (Data in Practice)
Case sensitivity of LIKE varies by database; PostgreSQL offers ILIKE for case-insensitive matching. Patterns that start with % can be slow on big tables, because the database can't use an index to jump to matching rows.
Step 10 of 15Practice · Diagram
Which prices does BETWEEN 15 AND 25 catch?
BETWEEN is inclusive: it keeps values equal to 15 and 25 as well as everything in between.
Which book is the cheapest one that BETWEEN 15 AND 25 still returns?
Tap the shape that answers the question, then press Check.
- Night Ferry
$12.99
- River Road
$14
- Gardens of Salt
$15.5
- The Silent Orbit
$18
- The Last Signal
$21
- Learning to Cook
$24
- Quiet Numbers
$35
- Data in Practice
$42
Step 11 of 15Flashcards
WHERE operators
Tap a card to flip it.
Step 12 of 15Practice · Code challenge · SQL
Code challenge · SQL
Return the title of every book whose genre is Tech or Sci-Fi and that was published in 2019 or later.
Watch the combination of AND and OR.
The database for this exercise (tables and rows)
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author TEXT NOT NULL,
genre TEXT,
price REAL,
year INTEGER,
stock INTEGER
);
INSERT INTO books VALUES
(1, 'The Silent Orbit', 'Mara Quinn', 'Sci-Fi', 18.00, 2019, 12),
(2, 'Gardens of Salt', 'Leo Park', 'Fiction', 15.50, 2021, 0),
(3, 'Data in Practice', 'Ana Ruiz', 'Tech', 42.00, 2022, 7),
(4, 'Night Ferry', 'Leo Park', 'Mystery', 12.99, 2017, 25),
(5, 'Learning to Cook', 'Sam Odell', 'Food', 24.00, 2020, 3),
(6, 'The Last Signal', 'Mara Quinn', 'Sci-Fi', 21.00, 2023, 9),
(7, 'Quiet Numbers', 'Ana Ruiz', 'Tech', 35.00, 2018, NULL),
(8, 'River Road', 'Jo Banks', 'Fiction', 14.00, 2016, 18);- Result matches the expected table
Step 13 of 15Practice · Match the pairs
Match each WHERE condition to the books it returns from the books table.
Tap an answer, then tap the place where it belongs (or drag it there).
- title LIKE '%Signal'
- price BETWEEN 14 AND 15.50
- author IN ('Sam Odell', 'Jo Banks')
- genre = 'Tech' AND year > 2020
Step 14 of 15Practice · Spot the mistake
This query should find Mystery or Fiction books that are in stock (stock greater than 0). Which lines contain mistakes? Click them.
Step 15 of 15Practice · Choose one
Which condition is equivalent to price >= 15 AND price <= 25?
Lesson complete!