Learn SQL
-- filtering.and_or_not

AND / OR / NOT in SQL

Combine conditions with Boolean logic

--concept

Complex filters chain simple conditions with three operators. AND requires every condition to be true; OR requires at least one; NOT reverses one. Precedence matters: AND binds tighter than OR, so salary > 60000 OR department = 'Sales' AND age < 30 quietly becomes “… OR (Sales AND young)” — always use parentheses to say what you mean.

--how it works
1

Three operators, one job each

AND = both true; OR = at least one true; NOT = invert. Everything complex is built from these.

2

Precedence is fixed

NOT binds tightest, then AND, then OR — salary > 60000 OR dept = 'Sales' AND age < 30 groups the AND first.

3

Parentheses are free clarity

WHERE (dept = 'Sales' OR dept = 'Marketing') AND age < 40 — say what you mean, even when redundant.

4

Short-circuit reading

Engines may stop evaluating an AND after the first FALSE — order cheap tests first as a habit.

5

NULL is the third truth value

Any comparison with NULL is UNKNOWN; WHERE keeps TRUE only, so UNKNOWN rows silently vanish.

--visualization
idnamedepartmentage1AliceEngineering292BobSales353CynthiaEngineering414DavidMarketing385ElenaSales266FaridEngineering33WHERE dept='Engineering' AND age<35
condition TRUE condition FALSE / dropped
Stage 1 / 8
Condition A: department = 'Engineering'. Condition B: age < 35. Operator: AND.
--example
query.sql
1SELECT name, department, salary, age
2FROM employees
3WHERE department = 'Engineering'
4 AND salary > 75000;
Result
namedepartmentsalaryage
AliceEngineering8500029
CynthiaEngineering9100041

Farid earns 73000 — Engineering ✓ but salary ✗ → filtered out. Toggle the operator in the visualization to see AND shrink the result while OR grows it.

--variants

AND narrows

Each added AND condition can only shrink the result.

query.sql
1SELECT name, department, salary
2FROM employees
3WHERE department = 'Engineering'
4 AND salary > 75000;
Result
namedepartmentsalary
AliceEngineering85000
CynthiaEngineering91000
1Alice: both ✓. Cynthia: both ✓. Farid: 73000 ✗.
2Three Engineering rows in, two out.
3AND = intersection of the two passing sets.

OR widens

Each added OR condition can only grow the result.

query.sql
1SELECT name, department
2FROM employees
3WHERE department = 'Sales'
4 OR salary > 80000;
Result
namedepartment
AliceEngineering
BobSales
CynthiaEngineering
ElenaSales
1Sales alone would be Bob and Elena; the salary clause adds Alice and Cynthia.
2Four rows pass one test or the other (or both — none here).
3OR = union of the two passing sets.

AND before OR

Why the parentheses matter.

query.sql
1SELECT name
2FROM employees
3WHERE department = 'Sales'
4 OR department = 'Marketing' AND salary > 60000;
Result
name
Bob
Elena
1AND binds first: Sales OR (Marketing AND salary > 60000).
2Marketing's only member, David, earns 47000 — the AND group matches nobody.
3So the result is just Sales: Bob and Elena — Cynthia's 91000 saves nobody because Engineering is on neither side.
4Wrap the OR in parentheses and the semantics flip: (Sales OR Marketing) AND salary > 60000 matches ZERO rows.

NOT inverts

Keep the complement of a condition.

query.sql
1SELECT name, department
2FROM employees
3WHERE NOT department = 'Engineering';
Result
namedepartment
BobSales
DavidMarketing
ElenaSales
1NOT flips TRUE↔FALSE; UNKNOWN stays UNKNOWN (and drops).
2The three Engineering rows fail NOT (they made the inner condition TRUE).
3Same result as department <> 'Engineering' — choose the clearer spelling.
--key facts
PrecedenceNOT → AND → OR; parentheses are free clarity
IN shortcutdept IN ('Sales','Marketing') replaces a chain of ORs
BETWEENis shorthand for x >= a AND x <= b
--use cases
Filter panels: category AND price-range AND in-stock
Access rules: (is_admin OR is_owner) AND not_deleted
Feature flags and segmentation queries in analytics
Debugging precedence bugs — always a missing pair of parentheses
--under the hood
Engines evaluate cheap, selective predicates first when it cannot hurt semantics — but write them in the order a human reads.
De Morgan's laws hold: NOT (a AND b) ≡ NOT a OR NOT b — handy for pushing NOT where an index can help.

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.