SQL WHERE Clause: Filter Rows With Conditions
Most questions are about some rows, not all of them: books under $20, customers in Denver, orders from last month. The WHERE clause keeps only the rows for which a condition is true.
Writing a condition
WHERE comes after FROM. The condition usually compares a column with a value using one of these operators:
=equal (one equals sign, unlike most programming languages)<>or!=not equal<,<=,>,>=less and greater than
Text values go in single quotes: WHERE genre = 'Fiction'. Numbers do not: WHERE price < 20. In SQLite, = on text is case-sensitive by default, so 'fiction' would match nothing here.
Expected output
title | price | year The Quiet Harbor | 14.99 | 2015 River of Salt | 17.25 | 2019 The Last Lantern | 12.5 | 2022 Paper Cranes | 11.0 | 2016
Filtering dates
SQLite has no separate date type. Dates are commonly stored as text in YYYY-MM-DD format, as in our orders.ordered_on column, and that format sorts and compares correctly as text. WHERE ordered_on >= '2025-10-01' therefore finds October 2025 orders and later. Other databases have real DATE columns, but the comparison looks the same.
The order SQL runs in
You write SELECT ... FROM ... WHERE, but the database logically processes FROM first, then WHERE, and only then SELECT. Two practical consequences:
- You can filter on a column you do not select: the example filters on
genrewithout showing it. - In most databases, a column alias defined in
SELECTcannot be used inWHERE, becauseWHEREruns first. SQLite is lenient and allows it, but writing the expression again keeps queries portable.
WHERE and missing values
Rows where the column is NULL (unknown) never match a comparison, not even <>. That gets its own chapter, because it surprises nearly everyone at first.
Common mistakes
- Using double quotes around text values. Some databases treat
"Fiction"as a column name. - Writing
==. SQLite happens to accept it, but standard SQL uses a single=. - Quoting numbers:
price < '20'compares against text in some databases and gives odd results.
Summary
WHERE conditionkeeps only rows where the condition is true.- Use
=,<>,<,<=,>,>=; quote text, not numbers. - ISO
YYYY-MM-DDdates compare correctly as text. WHEREis evaluated beforeSELECT.
Quiz: WHERE filters