Where
In the last lesson you chose which columns to return. Now you'll choose which rows . By the end of this lesson you'll filter any table with WHERE — comparing values, combining conditions with AND / OR / NOT , matching ranges and lists, finding text patterns, and handling missing data correctly.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free SQL course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
This is the same products table from the SELECT lesson, so the results stay consistent. Every query below filters these six rows — keep them in front of you as you read.
A WHERE clause adds a condition to a query. The database checks that condition once for each row and keeps only the rows where it is true. Everything else is filtered out.
WHERE is the bouncer at a club. Each row walks up; if it meets the rule ( price 30 ), it gets in; if not, it's turned away. The table itself is never changed — you're just choosing who gets through for this one query.
There are six comparison operators. Note that "not equal to" is written in standard SQL (most databases also accept != ):
When you compare against text , wrap the value in single quotes ( 'Electronics' ). Numbers like 30 are written bare, with no quotes.
Fill in the blanks to list the name and price of every Electronics product. Remember: column name on the left, the text value in single quotes on the right. The expected result is in the comments so you can check yourself.
One condition is rarely enough. Logical operators let you chain several together:
AND — both must be true
"Electronics and under $20" → narrows the result down.
OR — either can be true
NOT — flips a condition
BETWEEN x AND y is shorthand for column x AND column y . It is inclusive : both end values count as matches. It reads cleanly and is perfect for prices, dates, and ages.
Fill in the two range values so the query returns products priced from $5 up to $20 (inclusive). Low value first, then the high value.
IN (a, b, c) is true when the column equals any value in the list. It replaces a long chain of OR s and is much easier to read.
LIKE searches text with two wildcards: % stands for any number of characters (including none), and _ stands for exactly one character. Use it for "starts with", "ends with", and "contains" searches.
NULL is SQL's way of saying "there is no value here" — unknown or missing. Because NULL is not really a value, you cannot test it with = or ; those always come back "unknown" and match nothing. Use IS NULL and IS NOT NULL instead.
Never write WHERE category = NULL — it silently returns zero rows. It's always WHERE category IS NULL .
They do the same thing — "not equal to". is the official SQL standard; != works in most databases too. Pick one and stay consistent.
Inclusive. price BETWEEN 10 AND 35 includes both 10 and 35. If you want to exclude an end, use plain comparisons like price 10 AND price 35 .
It depends on the database. SQLite and MySQL are usually case-insensitive for ASCII letters; PostgreSQL's LIKE is case-sensitive (use ILIKE there). To be safe, match the case of your data.
Q: Why does WHERE category = NULL return nothing?
NULL means "unknown", and comparing anything to "unknown" gives "unknown" — never true. That's why you need IS NULL instead of = .
Put it all together — a brief, a blank canvas, and the expected result in the comments. Write it, then copy it into a playground to confirm.
Practice quiz
What does the WHERE clause do?
- Sorts the rows
- Renames columns
- Keeps only rows where its condition is true
- Removes duplicate rows
Answer: Keeps only rows where its condition is true. WHERE checks its condition once per row and keeps only the rows where it is true.
In standard SQL, how is 'not equal to' written?
- <>
- ==
- =/=
- NOT=
Answer: <>. Standard SQL writes 'not equal to' as <>; many databases also accept !=.
What does BETWEEN 10 AND 35 match?
- Values greater than 10 and less than 35 only
- Only the values 10 and 35
- Values not in the range
- Values from 10 to 35, including both ends
Answer: Values from 10 to 35, including both ends. BETWEEN is inclusive: both end values count, so it matches 10 through 35 inclusive.
When you mix AND and OR, why use parentheses?
- They are required for all WHERE clauses
- AND binds tighter than OR, so grouping makes intent clear
- OR binds tighter than AND
- Parentheses sort the result
Answer: AND binds tighter than OR, so grouping makes intent clear. AND is evaluated before OR, so A OR B AND C means A OR (B AND C); parentheses fix the grouping.
How do you correctly test for a missing (NULL) value?
- WHERE col IS NULL
- WHERE col = NULL
- WHERE col <> NULL
- WHERE col == NULL
Answer: WHERE col IS NULL. NULL cannot be tested with =; use IS NULL (or IS NOT NULL) instead.
What is IN ('Kitchen', 'Home') shorthand for?
- category = 'Kitchen' AND category = 'Home'
- category BETWEEN Kitchen AND Home
- category = 'Kitchen' OR category = 'Home'
- NOT category = 'Kitchen'
Answer: category = 'Kitchen' OR category = 'Home'. IN (a, b) is true when the column equals any value in the list — a tidy chain of ORs.
In LIKE patterns, what does the % wildcard match?
- Exactly one character
- Any number of characters, including zero
- Only digits
- A literal percent sign only
Answer: Any number of characters, including zero. % matches any number of characters (including none); _ matches exactly one character.
Which pattern finds product names that END with 'Mouse'?
- LIKE 'Mouse%'
- LIKE 'Mouse'
- LIKE '_Mouse_'
- LIKE '%Mouse'
Answer: LIKE '%Mouse'. Putting % before the text means 'any characters, then Mouse' — i.e. ends with 'Mouse'.
Why does WHERE category = NULL return zero rows?
- NULL is always zero
- Comparing anything to NULL yields 'unknown', never true
- It is a syntax error
- category has no values
Answer: Comparing anything to NULL yields 'unknown', never true. NULL means 'unknown', and comparing a value to unknown is never true, so no rows match.
Which logical operator widens a result by accepting either condition?
- AND
- NOT
- OR
- BETWEEN
Answer: OR. OR is true when either condition is true, widening the set of matching rows.