Complex WHERE Conditions & Boolean Logic: Mistakes
Module: Advanced Filtering
SELECT * FROM products WHERE category = 'electronics' OR category = 'computers' AND price < 1000;
SELECT * FROM products WHERE (category = 'electronics' OR category = 'computers') AND price < 1000;
Without parentheses, AND binds tighter than OR, giving: (electronics) OR (computers AND price<1000). Wrong logic!
Always use parentheses with mixed AND/OR
High
Missing parentheses causes wrong evaluation order
SELECT * FROM customers WHERE age > 18 AND age < 18;
SELECT * FROM customers WHERE age = 18; -- or remove impossible condition
age cannot be both >18 AND <18 simultaneously. This is a logic error.
Check for contradictory conditions
High
Impossible condition returns no results