Lesson 3 of 17 · First Queries
Filtering With WHERE
Filter rows in SQL with WHERE. Learn comparison operators, AND, OR and NOT, and the IN, BETWEEN and LIKE shortcuts, with queries you can run.
- 8 min read
- Beginner
- Reviewed
- How to keep only the rows that match a condition
- The comparison operators
- How to combine conditions with AND, OR and NOT
- The IN, BETWEEN and LIKE shortcuts
Asking for some rows, not all
SELECT chooses columns. WHERE chooses rows. It comes after FROM and holds a condition. The database checks the condition against every row and keeps only the rows where it is true.
SELECT name, country, age FROM students WHERE country = 'India';
Twelve students went in. Two came out.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | country = 'India' |
<> | Not equal to | country <> 'India' |
> | Greater than | age > 25 |
< | Less than | age < 25 |
>= | Greater than or equal to | age >= 25 |
<= | Less than or equal to | age <= 25 |
SELECT name, age FROM students WHERE age >= 25;
Numbers are written as they are. Text and dates go in single quotes.
SELECT name, joined FROM students WHERE joined >= '2026-04-01';
Dates written as year-month-day compare correctly, which is one reason that format is used everywhere in data work.
Combining conditions
AND keeps a row only when both conditions are true. OR keeps it when at least one is true.
SELECT name, country, age FROM students WHERE age < 25 AND country = 'India';
SELECT name, country FROM students WHERE country = 'Japan' OR country = 'Brazil';
NOT reverses a condition.
SELECT title, category FROM courses WHERE NOT category = 'Programming';
When you mix AND and OR, use brackets to make your meaning clear. AND is applied before OR, which often surprises people.
SELECT name, country, age FROM students WHERE (country = 'India' OR country = 'Spain') AND age > 22;
Remove the brackets and run it again. You get an extra row, because the query now means "India, or Spain and older than 22".
IN: matching a list
IN is a shorter way to write several OR conditions on the same column.
SELECT name, country
FROM students
WHERE country IN ('India', 'Japan', 'Brazil');BETWEEN: matching a range
BETWEEN includes both ends of the range.
SELECT title, hours FROM courses WHERE hours BETWEEN 8 AND 10;
LIKE: matching part of a text
LIKE compares text with a pattern. Two special characters do the work:
%stands for any number of characters, including none_stands for exactly one character
SELECT name FROM students WHERE name LIKE 'A%'; -- starts with A SELECT title FROM courses WHERE title LIKE '%for%'; -- contains "for" SELECT name FROM students WHERE name LIKE '_a%'; -- second letter is a
Common mistakes
List the name and age of students who are older than 22 and younger than 28.
-- Show the name and age of every student who is -- older than 22 AND younger than 28
Which rows does WHERE hours BETWEEN 8 AND 10 keep?
Summary
WHEREkeeps only the rows that match a condition.- Compare with
=,<>,>,<,>=and<=. - Combine conditions with
AND,ORandNOT, and use brackets when mixing them. INmatches a list,BETWEENa range andLIKEa text pattern.
Next: putting the result in the order you want.
Finished reading? Tick it off to track your progress.