SQL from Zero › Module 4: DQL: asking questions
Filtering rows with WHERE
Keep only the rows you care about, like books under $10 or orders still pending.
Lesson 19 of 33 · about 15 minutes
Video: WHERE in 3 minutesThe narrated video for this lesson is coming soon. The lesson below covers the same ideas.
The lesson
So far every query has returned every row. WHERE lets you keep only the rows you care about.
Add WHERE after FROM, followed by a condition. The database checks the condition on every row and keeps only the rows where it is true.
SELECT title, price
FROM books
WHERE price < 12;
Text values go in single quotes: WHERE genre = 'Fantasy'. Numbers don't need quotes. You can sort the filtered result as usual with ORDER BY, which always comes after WHERE.
| Operator | Meaning |
|---|---|
= | equal to |
<> | not equal to |
< and > | less than, greater than |
<= and >= | less than or equal to, greater than or equal to |
Key ideas
- WHERE keeps only the rows where the condition is true.
- The order is SELECT, FROM, WHERE, then ORDER BY.
- Text goes in single quotes and is case-sensitive; numbers don't need quotes.
Practise
Loading practice…
Quick check
1. Which query returns books published before 1950?
2. How many rows does this return?
SELECT * FROM orders WHERE status = 'pending';