Learn SQL: WHERE
Filtering rows with WHERE
Vic drops a stack of tapes on the counter. "A customer is asking for horror films. Do I really have to go through all 25 titles?" Not while you're here.
WHERE keeps only what's true
With WHERE you put a condition on the rows. The database tests that condition for every row and keeps only the rows for which it is true.
Text goes between single quotes: 'horror'. Without quotes, SQL thinks you mean a column.
SELECT title, genre, year FROM films WHERE genre = 'horror'| title | genre | year |
|---|---|---|
| Alien | horror | 1979 |
| The Shining | horror | 1980 |
Numbers without quotes
Numbers you just write as they are, without quotes. WHERE always comes after FROM: first you say which table, then which rows.
This query finds every film from 2023.
SELECT title, year, price_per_day FROM films WHERE year = 2023| title | year | price_per_day |
|---|---|---|
| Barbie | 2023 | 3.5 |
| Oppenheimer | 2023 | 3.5 |
| Het smelt | 2023 | 3 |
Try it yourself
Which members live in Mechelen? Show their name and plan.
Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson