WHERE is the core tool for turning a broad table scan into a precise business question. Learn to combine comparisons safely, reason about AND/OR, and understand which rows qualify before you move on to more advanced filtering.
Key Takeaways at a Glance:
- •The WHERE clause filters rows early in execution, before grouping or projection takes place.
- •Predicates evaluate to TRUE, FALSE, or UNKNOWN; WHERE retains ONLY rows where the condition evaluates strictly to TRUE.
- •Operator precedence: AND has higher precedence than OR. Always use explicit parentheses to avoid logic bugs.
- •WHERE cannot contain aggregate functions (e.g. WHERE COUNT(*) > 5); group-level filtering must be done in HAVING.
In Simple Words (The Mental Model)
The WHERE clause acts as a sieve or filter at the river mouth. As rows flow out of the table, the database checks each row against your conditions. If the condition is TRUE, the row passes through. If it is FALSE or UNKNOWN, the row is discarded immediately.
Real-World Analogy
Think of an airport security checkpoint. The security agent checks each passenger's ticket (the WHERE condition: 'has_boarding_pass = TRUE AND flight_date = TODAY'). Passengers who don't meet the condition are turned away immediately; they never even reach the departure gate.