WHERE
WHERE filters rows before anything else happens: SELECT * FROM orders WHERE amount > 100 returns only orders above 100. The condition can use =, <>, <, >, <=, >=, LIKE, IN, BETWEEN, IS NULL and can be combined with AND, OR and NOT.
Syntax
SELECT columns FROM table WHERE condition;| Part | What it is |
|---|---|
condition | An expression that is true, false or NULL for each row; only true rows are kept |
Examples
SELECT id, amount FROM orders WHERE status = 'paid';Result: Only orders whose status is exactly paid
Text goes in single quotes. In PostgreSQL the comparison is case sensitive, so 'Paid' would not match.
SELECT name, salary FROM employees WHERE salary >= 5000 AND dept = 'Sales';Result: Sales employees earning 5000 or more
SELECT name FROM customers WHERE country IN ('Aruba', 'Curacao') OR country IS NULL;Result: Customers from either island plus those with no country recorded
Common mistakes
- WHERE column = NULL never matches anything because NULL is not equal to anything; write WHERE column IS NULL.
- Mixing AND and OR without parentheses gives surprising rows because AND binds tighter; write WHERE (a OR b) AND c.
- Using a column alias from the SELECT list in WHERE fails (no such column) because WHERE runs before SELECT; repeat the expression or use a subquery.
Practice WHERE
These drills check your answer on live data, free, no account needed.
- Filter rowsReturn the name and salary of employees who earn more than 90,000.
- Small ordersReturn the id and amount of every order under 100, whatever the status.
Questions
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY and can use aggregates such as COUNT(*) or SUM(amount).
Can I use WHERE with a date?
Yes. Compare against an ISO date string: WHERE order_date >= '2025-01-01' AND order_date < '2025-02-01'. Keep dates in YYYY-MM-DD format so text comparison also sorts correctly in SQLite.
Related
- AND, OR, NOTHow to combine WHERE conditions with AND, OR and NOT, why AND binds before OR, how parentheses fix it, and how NULL changes the result of a logical test.
- LIKELIKE matches text against a pattern: % stands for any run of characters and _ for one character. Starts with, ends with, contains, case sensitivity, NOT LIKE.
- INWHERE column IN (...) keeps rows whose value appears in a list or in a subquery result. Examples, NOT IN, the NULL trap, and when to use EXISTS instead.
- BETWEENBETWEEN low AND high keeps values from low to high, both ends included. Examples with numbers and dates, the end-of-day problem with timestamps, NOT BETWEEN.