WHERE — Filtering Rows in SQL
Keep only the rows that match a condition
WHERE filters rows before anything is returned. It evaluates a condition against every row and keeps the ones where the condition is true. Conditions compare columns to values (=, >, <, >=, <=, !=), and can be combined with AND/OR/NOT. WHERE runs before grouping, which is why it cannot see aggregate results — that job belongs to HAVING.
State the condition
WHERE department = 'Engineering' — a per-row test written after FROM.
The engine tests every row
Conceptually each row is fed through the condition; conceptually a gate that opens only for TRUE.
TRUE passes, the rest drops
FALSE rows are out — and so are NULL rows, because an unknown comparison is not TRUE. That NULL rule surprises everyone once.
Combine with AND / OR / NOT
AND narrows, OR widens, NOT inverts; parentheses make precedence explicit.
Filter early, before grouping
WHERE runs before GROUP BY — which is also why it cannot see aggregates; that is HAVING's job.
1SELECT name, department, salary2FROM employees3WHERE department = 'Engineering';
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
| Farid | Engineering | 73000 |
Three of six rows survive the filter. In the visualization, non-matching rows bounce off the gate while matching rows pass through to the result.
Equality on text
Exact match against a quoted string.
1SELECT name, department, salary2FROM employees3WHERE department = 'Engineering';
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
| Farid | Engineering | 73000 |
Numeric comparison
Ordering operators work exactly as in arithmetic.
1SELECT name, salary2FROM employees3WHERE salary >= 70000;
| name | salary |
|---|---|
| Alice | 85000 |
| Cynthia | 91000 |
| Farid | 73000 |
AND — narrowing
Both conditions must hold.
1SELECT name, department, salary2FROM employees3WHERE department = 'Engineering'4 AND salary > 75000;
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
OR — widening
At least one condition must hold.
1SELECT name, department2FROM employees3WHERE department = 'Sales'4 OR department = 'Marketing';
| name | department |
|---|---|
| Bob | Sales |
| David | Marketing |
| Elena | Sales |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.