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 20 of 35 · about 15 minutes

Video: WHERE in 3 minutes

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.

OperatorMeaning
=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';

3. What's wrong with WHERE genre = Mystery?