Foundations: How Databases ThinkLesson 1.3
Filtering rows with WHERE — operators and conditions
WHERE clause, comparison operators, BETWEEN, IN, LIKE, NULL handling, IS NULL, IS NOT NULL
WHERE Cuts Rows Before SELECT Runs
WHERE tells the database which rows to keep. Every row is tested against the condition — rows that don't match are dropped before the result is assembled.
-- Basic comparison
SELECT name, price FROM products WHERE price > 100;
-- Range shorthand
SELECT name FROM products WHERE price BETWEEN 50 AND 200;
-- Match a list of values
SELECT name FROM products WHERE category IN ('Electronics', 'Books');
-- Pattern match (% = any chars, _ = one char)
SELECT name FROM products WHERE name LIKE 'Air%';NULL Is Not Zero or Empty String
NULL means unknown. Comparing NULL with = or != always returns false. Use IS NULL and IS NOT NULL instead:
-- Wrong: returns no rows even where discount is null
SELECT name FROM products WHERE discount = NULL;
-- Correct
SELECT name FROM products WHERE discount IS NULL;Combining Conditions
Use AND, OR, and NOT to combine conditions. AND requires both sides to be true. OR requires either side. Wrap OR conditions in parentheses to avoid precedence bugs:
SELECT name FROM products
WHERE category = 'Electronics' AND (price < 500 OR on_sale = true);Without the parentheses, AND binds tighter than OR and the logic changes.
