Back in college vivas, I used to think I knew the SQL WHERE clause pretty well.
I knew we use it to filter records, I remembered operators like =, <, >, and with that, my confidence was sorted.
But then someone would casually ask,
"Okay, explain LIKE or BETWEEN with proper syntax."
I knew the theory part. I knew what they do.
But when it came to writing the actual query, I'd mess up the syntax or forget small details.
So recently, I finally decided to clear this once and for all. I watched a good YouTube explanation, revised everything properly, and now I'm summarizing the entire WHERE clause world here β mainly for revision and future reference.
If you skim this article, you should get a clear picture of:
- what each WHERE operator does
- when to use it
- and how the syntax actually looks in real queries
Let's break it down in a simple, practical way.
Let's go.
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;
This brings everyone.
SELECT * FROM students
WHERE marks > 80;
This brings only the smart kids.
That's it. That's the purpose.
Big Picture: WHERE Operators (Mental Map)
Think of WHERE operators in 5 categories:
- Comparison Operators β compare values
- Logical Operators β combine conditions
- Range Operator β between two values
- Membership Operator β check from a list
- Search Operator β pattern matching
Now let's break them one by one.
Comparison Operators (The Ones Everyone Knows⦠Mostly)
These compare one value with another.
Operators
-
=β equal -
!=or<>β not equal -
>β greater than -
<β less than -
>=β greater than or equal -
<=β less than or equal
Syntax
SELECT columns
FROM table
WHERE condition;
Examples
SELECT * FROM students
WHERE age = 20;
SELECT * FROM students
WHERE marks > 75;
SELECT * FROM employees
WHERE salary <= 30000;
Nothing scary here. This is the confidence booster part.
Logical Operators (Where Things Start Combining)
Logical operators are used when one condition is not enough.
Operators
- AND β all conditions must be true
- OR β any one condition true
- NOT β reverse the condition
AND β Both Conditions Must Be True
SELECT * FROM students
WHERE marks > 80 AND age < 22;
Student must be smart AND young.
OR β Any One Condition Is Enough
SELECT * FROM students
WHERE city = 'Delhi' OR city = 'Mumbai';
Either Delhi or Mumbai. Chill.
NOT β The Opposite Game
SELECT * FROM students
WHERE NOT city = 'Delhi';
Everyone except Delhi.
BETWEEN (The One I Always Knew⦠But Forgot Syntax)
BETWEEN is used for ranges.
Important Truth: BETWEEN includes both values.
Syntax
WHERE column BETWEEN value1 AND value2;
Example
SELECT * FROM students
WHERE marks BETWEEN 60 AND 80;
Marks 60, 61, 62 β¦ 80 all included.
Equivalent to:
WHERE marks >= 60 AND marks <= 80;
But cleaner. Less typing. More peace.
IN / NOT IN (Cleaner Than Multiple ORs)
Instead of writing this monster:
WHERE city = 'Delhi' OR city = 'Mumbai' OR city = 'Pune';
Use IN like a sane developer.
IN β Value Exists in a List
SELECT * FROM students
WHERE city IN ('Delhi', 'Mumbai', 'Pune');
NOT IN β Value NOT in the List
SELECT * FROM students
WHERE city NOT IN ('Delhi', 'Mumbai');
Everyone except these cities.
Pro tip: If you see multiple ORs β IN is waiting for you.
LIKE (The Syntax That Betrayed Me in Exams)
LIKE is used for pattern matching, mostly with strings.
Wildcards You MUST Remember
-
%β zero or more characters -
_β exactly one character
Syntax
WHERE column LIKE pattern;
Examples (Read Slowly)
Starts with 'A'
SELECT * FROM students
WHERE name LIKE 'A%';
Aanya, Aman, Akash
Ends with 'a'
SELECT * FROM students
WHERE name LIKE '%a';
Riya, Sanya, Neha
Contains 'an'
SELECT * FROM students
WHERE name LIKE '%an%';
Ananya, Sandeep
Exactly 4 Letters
SELECT * FROM students
WHERE name LIKE '____';
Four underscores = four characters.
This is where most people mess up β not conceptually, but syntactically.
Final Mental Cheat Sheet
SELECT * FROM table
WHERE condition;
- Compare β =, >, <, >=, <=
- Combine β AND, OR, NOT
- Range β BETWEEN x AND y
- List β IN (a, b, c)
- Pattern β LIKE '%text%'
I genuinely thought I knew the WHERE clause.
But knowing 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.
Top comments (0)