Filtering Cheat Sheet
All WHERE operators on one page, plus a combined practice query that uses several of them together.
You now know every WHERE operator family. This page puts them side by side so you can pick the right one quickly — and shows how they combine in one real query.
Definition recap
A WHERE condition is built as expression + operator + expression, can be combined with AND / OR / NOT, and keeps only rows for which the final result is TRUE. WHERE comes after FROM and filters rows before grouping.
Cheat sheet
| Operator | Use it for | Example |
|---|---|---|
= | exact match | team = 'MI' |
<> / != | everything except | team <> 'MI' |
> >= < <= | compare size (>= / <= include the boundary) | runs >= 500 |
AND | all conditions must be TRUE | country = 'India' AND runs > 400 |
OR | at least one condition TRUE | team = 'MI' OR runs >= 800 |
NOT | reverse a condition | NOT country = 'India' |
BETWEEN a AND b | inside a range, both ends included | runs BETWEEN 500 AND 750 |
IN ('a', 'b') | match any value in a list | team IN ('MI', 'CSK') |
NOT IN (...) | match none of the list (beware NULLs!) | team NOT IN ('MI', 'CSK') |
LIKE | text pattern: % = anything, _ = one character | name LIKE '_o%' |
IS NULL / IS NOT NULL | missing / present values | nickname IS NULL |
Which operator should I use?
| Question in English | SQL |
|---|---|
| "exactly X" | = |
| "anything but X" | <> |
| "more than / at least" | > / >= |
| "from A to B" | BETWEEN A AND B |
| "one of these values" | IN (...) |
| "starts with / ends with / contains" | LIKE 'X%' / '%X' / '%X%' |
| "is missing" | IS NULL |
| "both rules" / "either rule" | AND / OR (with brackets when mixed) |
Example — several operators together
"Indian players from MI, RCB or CSK with 300 to 800 runs, whose name contains an 'h'":
sql
SELECT name, team, runs
FROM players
WHERE country = 'India'
AND team IN ('MI', 'RCB', 'CSK')
AND runs BETWEEN 300 AND 800
AND name LIKE '%h%';| name | team | runs |
|---|---|---|
| Virat Kohli | RCB | 750 |
| Rohit Sharma | MI | 500 |
| MS Dhoni | CSK | 350 |
How it works: FROM loads 6 rows → each row must pass all four tests (AND) → Buttler and Warner fail the country test, Bumrah fails BETWEEN (20 runs) → SELECT shows three columns.
Execution order reminder
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY
WHERE runs early, so it reduces rows before any grouping, aggregation or sorting happens — which is also why filtering in WHERE is efficient.
Common mistakes (all chapter)
- ❌ Text without single quotes. ✅
'India' - ❌
= NULL. ✅IS NULL - ❌ Mixing AND/OR without brackets. ✅
(A OR B) AND C - ❌
BETWEEN high AND low. ✅ low first - ❌
NOT INon a subquery that may contain NULL. ✅ filter NULLs or useNOT EXISTS - ❌
=with wildcards. ✅LIKE
Interview tip
Q: Rewrite WHERE team = 'MI' OR team = 'CSK' OR team = 'RR' more cleanly.
A: WHERE team IN ('MI', 'CSK', 'RR') — same result, shorter, and no precedence trap if you later add an AND.
Key points to remember
- Comparison: = <> != > >= < <= compare two values.
- Logical: AND (all true), OR (any true), NOT (reverse); use brackets when mixing AND and OR.
- Range: BETWEEN a AND b includes both ends.
- Membership: IN / NOT IN check a list — watch out for NULLs inside NOT IN.
- Search: LIKE with % (anything) and _ (exactly one character).
- IS NULL / IS NOT NULL are the only correct tests for missing values.
- WHERE comes after FROM and filters rows before GROUP BY, SELECT and ORDER BY.
Try it yourself
Four operators in one WHERE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Mixing AND, OR and brackets on innings
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: build your own filter
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
