TetSharing
SQL tutorial · 14 chapters
  1. Get started
  2. SELECT columns
  3. WHERE filters
  4. AND, OR, NOT, IN, BETWEEN
  5. ORDER BY and LIMIT
  6. LIKE patterns
  7. NULL values
  8. COUNT, SUM, AVG, MIN, MAX
  9. GROUP BY and HAVING
  10. INNER JOIN
  11. LEFT JOIN
  12. Subqueries
  13. INSERT, UPDATE, DELETE
  14. CREATE TABLE and constraints

Final SQL quiz
Cheat sheet: sql cheat sheet

Chapter 3 of 14 · By Ethan Cole · Updated November 21, 2026

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.

SQLite · editable

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 genre without showing it.
  • In most databases, a column alias defined in SELECT cannot be used in WHERE, because WHERE runs 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 condition keeps only rows where the condition is true.
  • Use =, <>, <, <=, >, >=; quote text, not numbers.
  • ISO YYYY-MM-DD dates compare correctly as text.
  • WHERE is evaluated before SELECT.

Quiz: WHERE filters

1. Which query finds books cheaper than 15?

WHERE follows FROM and filters rows.

2. Which operator means "not equal" in standard SQL?

<> is standard; SQLite also accepts !=.

3. Can you filter on a column that is not in the SELECT list?

WHERE is evaluated before SELECT, with access to every column.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.