WHERE: filtering rows
Filter SQL results with WHERE: compare numbers, text and dates, combine conditions with AND and OR, and use IN, BETWEEN and LIKE pattern matching.
🎯 By the end of this lesson you will be able to:
- Keep only the rows you need with WHERE
- Combine conditions with AND, OR and brackets
- Use IN, BETWEEN and LIKE to write shorter, clearer filters
Outline
What you'll learn and why it matters
SELECT chooses columns; WHERE chooses rows. Almost every real question has a filter in it: customers in India, products under ₹300, orders since October. WHERE is the clause you will write more than any other.
Your first filter
SELECT name, city
FROM customers
WHERE country = 'India';Result: 4 rows
| name | city |
|---|---|
| Asha Patel | Mumbai |
| Ravi Kumar | Bengaluru |
| Meera Nair | Kochi |
| Arjun Singh | Delhi |
The database looks at each of the 10 customers and keeps the 4 rows where country is 'India'. Text values go in single quotes. Numbers do not need quotes.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= | equal to | country = 'UK' |
<> or != | not equal to | status <> 'cancelled' |
< > | less than, greater than | price < 300 |
<= >= | less/greater than or equal to | quantity >= 2 |
SELECT name, price
FROM products
WHERE price < 300;Result: 4 rows
| name | price |
|---|---|
| Notebook A5 | 120 |
| Gel Pens (pack of 5) | 90 |
| USB-C Cable | 299 |
| Coffee Mug | 249 |
Dates in our database are stored as text in the form YYYY-MM-DD. Because the year comes first, comparing them as text also puts them in date order, so this finds orders from October 2025 onwards:
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date >= '2025-10-01';Result: 5 rows
| order_id | customer_id | order_date |
|---|---|---|
| 11 | 9 | 2025-10-02 |
| 12 | 1 | 2025-10-05 |
| 13 | 4 | 2025-10-08 |
| 14 | 5 | 2025-10-11 |
| 15 | 3 | 2025-10-15 |
Combining conditions: AND and OR
AND keeps a row only when both conditions are true. OR keeps it when at least one is true.
SELECT name, category, price
FROM products
WHERE category = 'Books' AND price < 400;Result: 1 row
| name | category | price |
|---|---|---|
| SQL Pocket Guide | Books | 350 |
SELECT name, category, price
FROM products
WHERE category = 'Books' OR category = 'Home';Result: 4 rows
| name | category | price |
|---|---|---|
| Python Basics Book | Books | 450 |
| SQL Pocket Guide | Books | 350 |
| Water Bottle | Home | 399 |
| Coffee Mug | Home | 249 |
Shortcuts: IN and BETWEEN
IN checks against a list, replacing a chain of ORs:
SELECT name, country
FROM customers
WHERE country IN ('UK', 'Canada', 'Australia');Result: 3 rows
| name | country |
|---|---|
| John Smith | UK |
| Sara Lopez | Canada |
| Emma Brown | Australia |
BETWEEN checks a range and includes both ends:
SELECT name, price
FROM products
WHERE price BETWEEN 200 AND 500;Result: 5 rows
| name | price |
|---|---|
| Python Basics Book | 450 |
| SQL Pocket Guide | 350 |
| USB-C Cable | 299 |
| Water Bottle | 399 |
| Coffee Mug | 249 |
Add NOT to reverse either one: country NOT IN ('India'), price NOT BETWEEN 200 AND 500.
Pattern matching with LIKE
LIKE matches text against a pattern. % stands for “any number of characters (including none)” and _ for “exactly one character”.
SELECT name
FROM products
WHERE name LIKE '%book%';Result: 2 rows
| name |
|---|
| Notebook A5 |
| Python Basics Book |
Look closely: Notebook matched too, because it contains “book”. Patterns match exactly what you write, so check your results. In SQLite, LIKE ignores the difference between capital and small letters for English letters; = does not.
| Pattern | Matches |
|---|---|
'A%' | starts with A |
'%a' | ends with a |
'%pen%' | contains pen anywhere |
'_i%' | second letter is i |
Text is matched exactly
SELECT name, city
FROM customers
WHERE country = 'india';Result: 0 rows
| name | city |
|---|---|
| (no rows) | |
No rows! With =, 'india' is not the same as 'India'. An empty result is not an error, which makes this mistake easy to miss. When a filter returns nothing, check capitals, spaces and spelling first.
Practice
Guided exercise
Bulk delivered orders
Find delivered orders where the customer bought more than one of the item. Show the order id, quantity and status.
- Select
order_id, quantity, statusfromorders. - Filter with
status = 'delivered'. - Add
AND quantity > 1.
Show the solution
SELECT order_id, quantity, status
FROM orders
WHERE status = 'delivered' AND quantity > 1;Result: 4 rows
| order_id | quantity | status |
|---|---|---|
| 2 | 3 | delivered |
| 4 | 2 | delivered |
| 9 | 5 | delivered |
| 13 | 2 | delivered |
How to get there: Both conditions must be true, so AND is right. Order 8 has a quantity of 2 but is only shipped, so it is left out.
Your turn
Early customers
Show the name and email of customers who joined before 1 April 2025.
Show the solution
SELECT name, email
FROM customers
WHERE joined_on < '2025-04-01';Result: 4 rows
| name | |
|---|---|
| Asha Patel | asha@example.com |
| Ravi Kumar | ravi@example.com |
| Meera Nair | NULL |
| John Smith | john@example.com |
How to get there: Dates written as YYYY-MM-DD compare correctly as text. < means strictly before, so someone who joined on 1 April would not be included.
Common mistakes
Double quotes around text: country = "India"
You see: Works in SQLite but fails in other databases, or is treated as a column name
Fix: Always use single quotes for text values.
Wrong capitals or extra spaces in text
You see: An empty result, no error
Fix: Match the stored value exactly, or use LIKE for a case-insensitive match.
Mixing AND and OR without brackets
You see: Extra rows you did not expect
Fix: Put brackets around the OR part.
Writing price > 200 AND < 500
You see: near "<": syntax error
Fix: Repeat the column: price > 200 AND price < 500, or use BETWEEN.
Recap
WHEREkeeps only the rows where its condition is true.- Compare with
=,<>,<,>,<=,>=. Text goes in single quotes. ANDneeds both conditions,ORneeds either. Use brackets when mixing them.INchecks a list,BETWEENan inclusive range, andLIKEa pattern with%and_.
Key terms
- Condition
- A test that is true or false for each row.
- Wildcard
- A symbol in a LIKE pattern: % for any characters, _ for one character.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which filter keeps products costing from 100 to 300, including both 100 and 300?
2. Which names does `WHERE name LIKE 'S%'` match?
3. `WHERE country = 'uk'` returns no rows, but there is a customer from the UK. Why?
4. Which is the same as `WHERE country = 'UAE' OR country = 'UK'`?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.