SQLWHERE Clause

WHERE Clause

The WHERE clause is how you filter which rows a query returns. Without it, SELECT returns every row in the table; add a WHERE clause and only the rows matching your condition come back.
Basic filtering

Filter rows by a condition

SQL
SELECT first_name, last_name, department
FROM employees
WHERE department = 'Engineering';

The database evaluates the condition once per row. If it evaluates to true for that row, the row is kept; otherwise it is dropped from the result before anything else happens to it.

WHERE runs before grouping
Recall the logical order SQL executes in: FROM, then WHERE, then GROUP BY, then HAVING, then SELECT, then ORDER BY. Because WHERE runs before any grouping happens, it filters individual, ungrouped rows — it has no awareness of groups or aggregate values like SUM() or COUNT() yet.

WHERE filters rows before grouping occurs

SQL
SELECT department, COUNT(*) AS headcount
FROM employees
WHERE hire_date >= '2020-01-01'
GROUP BY department;
Here WHERE narrows the rows down to employees hired since 2020 *before* they get grouped and counted by department.
WHERE vs HAVING — a classic mix-up
WHERE filters individual rows, before grouping. HAVING filters the groups produced by GROUP BY, after grouping. Trying to filter on an aggregate like COUNT(*) or SUM(...) inside a WHERE clause will fail, because that aggregate value does not exist yet at the point WHERE runs. We cover HAVING in detail on its own page.
Combining conditions
A WHERE clause can combine multiple conditions using AND, OR, and NOT, and can use operators like BETWEEN, IN, and LIKE for more expressive filters. Each of these gets its own dedicated page next in this series.
Column order doesn't matter in a WHERE condition
WHERE salary > 50000 and WHERE 50000 < salary filter identically — what matters is the logical comparison, not which side of the operator a value sits on.