Learn SQL
-- filtering.where

WHERE — Filtering Rows in SQL

Keep only the rows that match a condition

--concept

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.

--how it works
1

State the condition

WHERE department = 'Engineering' — a per-row test written after FROM.

2

The engine tests every row

Conceptually each row is fed through the condition; conceptually a gate that opens only for TRUE.

3

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.

4

Combine with AND / OR / NOT

AND narrows, OR widens, NOT inverts; parentheses make precedence explicit.

5

Filter early, before grouping

WHERE runs before GROUP BY — which is also why it cannot see aggregates; that is HAVING's job.

--visualization
WHERE department =
idnamedepartmentsalary1AliceEngineering850002BobSales520003CynthiaEngineering910004DavidMarketing470005ElenaSales580006FaridEngineering73000WHERE gate
namedepartmentsalary
AliceEngineering85000
CynthiaEngineering91000
FaridEngineering73000
TRUE — kept FALSE — dropped
Stage 1 / 8
WHERE department = 'Engineering' — the engine tests every row against the condition.
--example
query.sql
1SELECT name, department, salary
2FROM employees
3WHERE department = 'Engineering';
Result
namedepartmentsalary
AliceEngineering85000
CynthiaEngineering91000
FaridEngineering73000

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.

--variants

Equality on text

Exact match against a quoted string.

query.sql
1SELECT name, department, salary
2FROM employees
3WHERE department = 'Engineering';
Result
namedepartmentsalary
AliceEngineering85000
CynthiaEngineering91000
FaridEngineering73000
1Six rows enter the gate, three are Engineering.
2The other three fail the equality test and are dropped.
3Text needs quotes; numbers never do.

Numeric comparison

Ordering operators work exactly as in arithmetic.

query.sql
1SELECT name, salary
2FROM employees
3WHERE salary >= 70000;
Result
namesalary
Alice85000
Cynthia91000
Farid73000
185000, 91000 and 73000 clear the 70000 bar.
252000, 58000 and 47000 do not.
3No quotes: numbers compare numerically, never lexically.

AND — narrowing

Both conditions must hold.

query.sql
1SELECT name, department, salary
2FROM employees
3WHERE department = 'Engineering'
4 AND salary > 75000;
Result
namedepartmentsalary
AliceEngineering85000
CynthiaEngineering91000
1Farid is Engineering but earns 73000 — fails the second test.
2AND shrinks results: each extra condition can only remove rows.
3Sales employees fail at the first condition and skip the second.

OR — widening

At least one condition must hold.

query.sql
1SELECT name, department
2FROM employees
3WHERE department = 'Sales'
4 OR department = 'Marketing';
Result
namedepartment
BobSales
DavidMarketing
ElenaSales
1Two departments pass — the union of two groups.
2OR grows results; AND shrinks them.
3For a same-column list like this, IN is the tidier spelling.
--key facts
NULL gotchaWHERE col = NULL never matches — use IS NULL / IS NOT NULL
Performancefiltered columns with indexes turn scans into instant lookups
Order of clausesSELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY
--use cases
Every search box, filter panel and drill-down in every app
Scoping aggregates: WHERE department = 'Sales' before AVG(salary)
Security: WHERE owner_id = :current_user to show people only their own rows
Debugging data: pull the exact rows a report complained about
--under the hood
An index on the filtered column turns a full scan into a targeted lookup — the single biggest lever on query speed.
Functions on the filtered column (WHERE YEAR(d) = 2024) defeat indexes; prefer range predicates (d >= '2024-01-01' AND d < '2025-01-01').

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.