2.1 Filtering Rows with WHERE, AND, OR, IN & BETWEEN
Master row-level filtering with WHERE, comparison operators, IN vs OR, BETWEEN vs half-open ranges for timestamps, sargable index-friendly filters, and the AND/OR precedence bug.
1. How WHERE Works Visually
Think of WHERE as a strict security gate: every row loaded by FROM is tested against your condition, and only rows that evaluate to TRUE pass through to the next stage.
2. Complete Comparison & Range Operator Reference
| Operator | Meaning | Example | Notes |
|---|---|---|---|
= | Exact equality | status = 'paid' | Case-sensitive in PostgreSQL, case-insensitive in default MySQL! |
<> or != | Not equal | status <> 'cancelled' | <> is ANSI SQL standard; != works in Postgres/MySQL/SQLite |
>, >=, <, <= | Numeric, date, or alphabetical order | amount >= 500 | Works on INT, DECIMAL, DATE, TIMESTAMP, VARCHAR |
IN (...) | Matches any value in a set | city IN ('Bengaluru', 'Pune') | Cleaner and easier for the optimizer than chained ORs |
NOT IN (...) | Excludes values in a set | role NOT IN ('banned', 'bot') | ⚠️ Watch out if the list contains NULL (Lesson 2.2!) |
BETWEEN a AND b | Inclusive range (>= a AND <= b) | price BETWEEN 100 AND 500 | Both 100 and 500 are included! |
3. Crucial Differences That Trip Up Developers
A. IN vs Multiple OR Conditions
-- ❌ Repetitive and error-prone
SELECT order_id, city FROM orders
WHERE city = 'Bengaluru' OR city = 'Mumbai' OR city = 'Pune' OR city = 'Delhi';
-- ✅ Clean, readable, and optimizer-friendly
SELECT order_id, city FROM orders
WHERE city IN ('Bengaluru', 'Mumbai', 'Pune', 'Delhi');
B. BETWEEN vs Half-Open Ranges (>= and <) for Dates/Timestamps
| Pattern | Example | Verdict |
|---|---|---|
BETWEEN on DATE | order_date BETWEEN '2026-11-01' AND '2026-11-30' | ✅ Fine when column is a pure DATE (no time part) |
BETWEEN on TIMESTAMP | created_at BETWEEN '2026-11-01' AND '2026-11-30' | ❌ BUG! Misses all orders on Nov 30 after 00:00:00! |
Half-open >= and < | created_at >= '2026-11-01' AND created_at < '2026-12-01' | ✅ Always exact for both DATE and TIMESTAMP (even leap years!) |
C. AND vs OR Operator Precedence (The Classic Bug!)
Just like * runs before + in math, NOT → AND → OR is SQL's strict order of precedence.
-- ❌ BUGGY: SQL reads this as: city = 'Delhi' OR (city = 'Mumbai' AND status = 'paid')
-- Returns unpaid Delhi orders by mistake!
SELECT order_id, city, status
FROM orders
WHERE city = 'Delhi' OR city = 'Mumbai' AND status = 'paid';
-- ✅ FIXED: Always wrap OR groups in parentheses!
SELECT order_id, city, status
FROM orders
WHERE (city = 'Delhi' OR city = 'Mumbai')
AND status = 'paid';
D. Index-Friendly ("Sargable") vs Index-Breaking WHERE Clauses
Never wrap an indexed column inside a function in WHERE — it forces the database to ignore your index and scan every single row!
-- ❌ SLOW: Wrapping created_at in YEAR() disables B+ Tree index lookup!
SELECT * FROM orders
WHERE EXTRACT(YEAR FROM created_at) = 2026;
-- ✅ FAST (Sargable): Leaves created_at bare so the B+ Tree index jumps right to 2026!
SELECT * FROM orders
WHERE created_at >= '2026-01-01'
AND created_at < '2027-01-01';
4. Watch Compound WHERE Filtering Execute Step-by-Step
| order_id | customer | city | amount |
|---|---|---|---|
| 501 | Aarav | Bengaluru | 1200 |
| 502 | Meera | Delhi | 900 |
| 503 | Kabir | Pune | 350 |
| 504 | Riya | Pune | 850 |
5. Practice Challenge
Write a query to find all orders placed in November 2026 (using a TIMESTAMP column created_at) that are either 'paid' or 'shipped' and have amount >= 1000.
6. Quick Check Your Understanding
Module 2.1 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Paste LeetCode-style level-order tree arrays, graph edge lists, or 2D matrices to render clean vector SVG diagrams and DFS traversals.
Convert Unix epoch seconds/milliseconds to ISO-8601 and local timezones, and bulk-generate UUID v4, time-ordered UUID v7, and sortable ULIDs.