SQL WHERE Clause: I Thought I Knew It… Until It Asked Me About LIKE 😭
Source: Dev.to
What is the WHERE Clause?
The WHERE clause is basically SQL’s filter button.
You don’t want all rows – you want specific rows.
SELECT * FROM students;
All rows
SELECT * FROM students
WHERE marks > 80;
Only the smart kids
That’s it. That’s the purpose.
Big Picture: WHERE Operators (Mental Map)
Think of WHERE operators in five categories:
| Category | Purpose |
|---|---|
| Comparison Operators | Compare values |
| Logical Operators | Combine conditions |
| Range Operator | Test a value between two bounds |
| Membership Operator | Check membership in a list |
| Search Operator | Pattern matching (LIKE) |
Now let’s break them down one by one.
Comparison Operators (The Ones Everyone Knows… Mostly)
These compare one value with another.
| Operator | Meaning |
|---|---|
= | equal |
!= or <> | not equal |
> | greater than |
>= | greater than or equal |
< | less than |
<= | less than or equal |
Logical Operators
| Operator | Meaning |
|---|---|
AND | Both conditions must be true |
OR | At least one condition must be true |
NOT | Negates a condition |
Range Operator
| Operator | Syntax |
|---|---|
BETWEEN | BETWEEN x AND y – checks if a value lies within a range (inclusive) |
Membership Operator
| Operator | Syntax |
|---|---|
IN | IN (a, b, c) – checks if a value matches any value in a list |
Search Operator
| Operator | Syntax |
|---|---|
LIKE | LIKE '%text%' – pattern matching with wildcards (% for any sequence, _ for a single character) |
Quick Reference Cheat Sheet
| Category | Example |
|---|---|
| Combine | AND, OR, NOT |
| Range | BETWEEN x AND y |
| List | IN (a, b, c) |
| Pattern | LIKE '%text%' |
Keep this sheet handy, and the WHERE clause will always be your friend.
I genuinely thought I knew the WHERE clause.
But knowing the names of operators and knowing how to write them correctly are two very different things.
This article is my revision note, my syntax reminder, and my future interview backup.