Level 2: Filtering rows with WHERE
Updated on
Beginner · level 2 of 20. Goal: Keep only the rows that meet a condition.
Topics: WHERE = <> < > <= >=
Lesson
A table often holds many more rows than the ones you care about. WHERE is used to filter: it goes after FROM and keeps only the rows for which the condition is true. The others disappear from the result, but of course they stay in the table.
A condition is a comparison. You compare a column with a value using = (equal), <> (different), < (less than), > (greater than), <= (less than or equal) and >= (greater than or equal). “At least 47000” is written >= 47000, “more than 47000” is written > 47000: the nuance changes the result as soon as a value is exactly 47000.
Text is written between single quotes: 'Drama'. Without quotes, SQL thinks Drama is a column name. A number is written without quotes: 2018. With =, text comparison is case-sensitive: 'drama' is not equal to 'Drama'.
You can also compare two columns of the same row: WHERE seats_sold < capacity keeps the flights that are not full. Or compare the result of a calculation: WHERE capacity - seats_sold > 40.
A key point for what follows: SQL does not run the query in the order it is written. It first takes the table (FROM), then filters the rows (WHERE), and only then builds the requested columns (SELECT). This order of execution explains many rules you will meet later. For example, an alias created in the SELECT does not exist yet when the WHERE runs: in standard SQL, you therefore write the calculation again in the WHERE.
Syntax
SELECT columns
FROM my_table
WHERE column >= value;Worked example
SELECT title, genre
FROM movies
WHERE genre = 'Drama';Only the movies whose genre is exactly 'Drama' are kept.
| id | title | genre | year | duration | rating | director_id |
|---|---|---|---|---|---|---|
| 1 | Night Train | Thriller | 2015 | 118 | 7.8 | 1 |
| 2 | Blue Harbor | Drama | 2018 | 102 | 7.1 | 2 |
| 3 | Paper Moon City | Comedy | 2012 | 95 | 6.4 | 5 |
| 4 | Silent Peak | Drama | 2020 | 131 | 8.2 | 3 |
| 5 | Last Signal | Sci-Fi | 2019 | 142 | 7.5 | 1 |
| 6 | Summer Keys | Comedy | 2016 | 88 | 5.9 | 4 |
| 7 | Iron Garden | Sci-Fi | 2021 | 125 | 8 | 3 |
| 8 | Dust and Gold | Western | 2014 | 110 | 6.8 | 2 |
| 9 | The Quiet Hour | Drama | 2022 | 97 | 7.4 | 5 |
| 10 | Deep Current | Thriller | 2017 | 105 | 6.9 | NULL |
Example result
| title | genre |
|---|---|
| Blue Harbor | Drama |
| Silent Peak | Drama |
| The Quiet Hour | Drama |
Key points
- WHERE always comes after FROM.
- Text between quotes, numbers without quotes.
- <> means “different from”.
Common pitfalls
- Writing WHERE genre = Drama without quotes: SQL then looks for a column named Drama.
- Mixing up > and >=: “at least 47000” is written >= 47000.
SQLite specifics
SQLite accepts a SELECT alias in the WHERE: WHERE empty_seats > 40 works. PostgreSQL, SQL Server and Oracle reject it. Build the portable habit: write the calculation again in the WHERE.
Another difference: with =, SQLite compares text case-sensitively (BINARY collation). MySQL, with its default collation, ignores case: 'drama' = 'Drama' is true there.
The level’s 5 exercises
- Show the title of the movies whose genre is 'Comedy'.
- Show the title and year of the movies released after 2018 (2018 excluded).
- Show the name and salary of the employees who earn at least 47000.
- Show the name and department of the employees who don't work in the 'IT' department.
- Show the airline, origin, destination and number of empty seats (empty_seats) of the flights with more than 40 empty seats.
In SpeedQL, every query is checked straight away: its result is compared with the expected one, then the query is run again on a hidden control database. Each exercise has written hints, to show only if you get stuck.
See also: the cheat sheet card · the SQL exercises with solutions on this topic
Do the level 2 exercises
Free, no sign-up: the lesson and the 5 exercises open right in your browser.
← Previous level: Reading a table with SELECT · Next level: Combining conditions →