WHERE
WHERE keeps the rows you want and leaves out the rest. SELECT chooses columns; WHERE chooses rows. "Which courses cost more than $400?", "Who lives in Texas?" and "Which payments came in December?" are all WHERE questions. This lesson covers the six comparison operators, how to write text and date values, filtering on a calculation, and the one surprise every beginner meets: rows with a missing value.
In One Minute
WHERE conditionkeeps a row only when the condition is true for it.- Compare with
=,<>(not equal),<,>,<=and>=. - Text and dates go in single quotes:
state = 'TX',paid_on >= '2025-12-01'. - You can filter on a calculation:
WHERE fee_usd * 0.9 < 400. - A row whose value is
NULLis left out of both= 'card'and<> 'card'; NULL Values explains why.
The Basics
SELECT columns
FROM table_name
WHERE condition
ORDER BY columns;WHERE goes after FROM and before ORDER BY. For each row, the database checks the condition; the row stays only if the condition is true. Here are the courses that cost more than $400:
SELECT title, fee_usd
FROM courses
WHERE fee_usd > 400
ORDER BY fee_usd DESC, title; title | fee_usd
-------------------+---------
Java Collections | 550.00
Java OOP | 500.00
Java Basics | 450.00
JavaScript Basics | 450.00
(4 rows)Four of the six courses pass; SQL Basics ($400.00) and Git Basics ($250.00) don't. 400 is not greater than 400, so SQL Basics is left out; >= would keep it.
The Comparison Operators
| Operator | Means | Example | True for |
|---|---|---|---|
= | Equal to | state = 'TX' | Navika |
<> | Not equal to | fee_usd <> 450 | Every course except the two at $450.00 |
< | Less than | fee_usd < 400 | Git Basics |
> | Greater than | fee_usd > 400 | Four courses |
<= | Less than or equal to | fee_usd <= 400 | SQL Basics, Git Basics |
>= | Greater than or equal to | paid_on >= '2025-12-01' | Payments from December on |
<> is the standard way to write "not equal". PostgreSQL, MySQL, SQL Server, Oracle and SQLite also accept !=, which means the same thing:
SELECT title, fee_usd
FROM courses
WHERE fee_usd <> 450
ORDER BY title; title | fee_usd
------------------+---------
Git Basics | 250.00
Java Collections | 550.00
Java OOP | 500.00
SQL Basics | 400.00
(4 rows)Text and Dates in Quotes
Text values and dates go in single quotes; numbers don't. Text comparisons are case-sensitive, as you saw in SQL Syntax, so 'TX' matches but 'tx' wouldn't:
SELECT first_name, city, state
FROM students
WHERE state = 'TX'; first_name | city | state
------------+--------+-------
Navika | Dallas | TX
(1 row)Write dates as 'YYYY-MM-DD', the ISO format, which all the major databases read the same way for a date column. Because paid_on is a date column, the text '2025-12-01' is read as a date and compared as one, so later dates are greater:
SELECT payment_id, amount_usd, paid_on
FROM payments
WHERE paid_on >= '2025-12-01'
ORDER BY paid_on; payment_id | amount_usd | paid_on
------------+------------+------------
3 | 250.00 | 2025-12-03
7 | 225.00 | 2025-12-19
4 | 550.00 | 2025-12-29
5 | 200.00 | 2026-01-05
(4 rows)Comparing Two Columns
A condition can compare two columns of the same row. Which payments were paid after their due date?
SELECT payment_id, due_on, paid_on
FROM payments
WHERE paid_on > due_on
ORDER BY payment_id; payment_id | due_on | paid_on
------------+------------+------------
3 | 2025-12-01 | 2025-12-03
7 | 2025-12-15 | 2025-12-19
(2 rows)For each payment, PostgreSQL compares that row's own paid_on with its own due_on. Payments 3 and 7 were late.
Filtering on a Calculation
The condition can use a calculation. If every fee dropped by 10%, which courses would cost less than $400?
SELECT title, fee_usd, fee_usd * 0.9 AS discounted
FROM courses
WHERE fee_usd * 0.9 < 400
ORDER BY title; title | fee_usd | discounted
------------+---------+------------
Git Basics | 250.00 | 225.000
SQL Basics | 400.00 | 360.000
(2 rows)The calculation is written out in WHERE, not by its alias. As you learned in Aliases, WHERE is evaluated before the SELECT list, so WHERE discounted < 400 would fail with column "discounted" does not exist.
Rows with a Missing Value
A condition is true, false, or unknown. When the value being compared is NULL, the comparison is unknown, and WHERE keeps only the true rows. So a row with a missing value is left out of a test and of its opposite. Payment 5 has no method recorded:
SELECT payment_id, method
FROM payments
WHERE method = 'card'
ORDER BY payment_id; payment_id | method
------------+--------
1 | card
2 | card
4 | card
6 | card
(4 rows)SELECT payment_id, method
FROM payments
WHERE method <> 'card'
ORDER BY payment_id; payment_id | method
------------+--------
3 | check
7 | check
(2 rows)Four card payments plus two that aren't card makes six, but there are seven payments. Payment 5 is in neither list, because "is a missing method equal to card?" has no yes-or-no answer. To find missing values, use IS NULL; NULL Values covers this fully.
Common Mistakes
Text without quotes. WHERE state = TX fails with column "tx" does not exist: without quotes, TX is read as a column name.
Using an alias in WHERE. Repeat the expression instead: WHERE fee_usd * 0.9 < 400.
Expecting <> to find missing values. method <> 'card' leaves out payment 5. Add OR method IS NULL if you want it.
Ambiguous date formats. Write '2025-12-01', not '12/01/2025'.
Interview Corner
What is the difference between WHERE and SELECT?
SELECT chooses which columns appear in the result; WHERE chooses which rows. WHERE is evaluated before the SELECT list, which is why it can't use a column alias defined there.
Why doesn't WHERE method <> 'card' return rows where method is NULL?
Comparing NULL with anything gives unknown, not true, and WHERE keeps only rows where the condition is true. Use WHERE method <> 'card' OR method IS NULL, or PostgreSQL's method IS DISTINCT FROM 'card'.
What does WHERE fee_usd > 400 return for a course that costs exactly 400?
Nothing: 400 is not greater than 400. Use >= to include the boundary.
Practice
Challenge: List the title and fee_usd of every course that costs $450.00 or less, cheapest first, breaking ties by title.
Show solution
SELECT title, fee_usd
FROM courses
WHERE fee_usd <= 450
ORDER BY fee_usd, title; title | fee_usd
-------------------+---------
Git Basics | 250.00
SQL Basics | 400.00
Java Basics | 450.00
JavaScript Basics | 450.00
(4 rows)<= includes the two $450.00 courses; < would have left them out.
Key Takeaways
| You want to | Write |
|---|---|
| Rows with a value above a number | WHERE fee_usd > 400 |
| Rows equal to a text value | WHERE state = 'TX' |
| Rows from a date on | WHERE paid_on >= '2025-12-01' |
| Compare two columns | WHERE paid_on > due_on |
| Filter on a calculation | WHERE fee_usd * 0.9 < 400 |
Was this tutorial helpful?