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
condition TRUE condition FALSE / dropped
Stage 1 / 8
Condition A: department = 'Engineering'. Condition B: age < 35. Operator: AND.
--example
query.sql
1SELECTname,department,salary,age
2FROMemployees
3WHEREdepartment='Engineering'
4ANDsalary>75000;
Result
name
department
salary
age
Alice
Engineering
85000
29
Cynthia
Engineering
91000
41
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
1SELECTname,department,salary
2FROMemployees
3WHEREdepartment='Engineering'
4ANDsalary>75000;
Result
name
department
salary
Alice
Engineering
85000
Cynthia
Engineering
91000
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
1SELECTname,department
2FROMemployees
3WHEREdepartment='Sales'
4ORsalary>80000;
Result
name
department
Alice
Engineering
Bob
Sales
Cynthia
Engineering
Elena
Sales
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
1SELECTname
2FROMemployees
3WHEREdepartment='Sales'
4ORdepartment='Marketing'ANDsalary>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.
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 →