DEV Community

Cover image for SQL WHERE Clause: I Thought I Knew It… Until It Asked Me About LIKE 😭
Yukti Sahu
Yukti Sahu

Posted on

SQL WHERE Clause: I Thought I Knew It… Until It Asked Me About LIKE 😭

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;
Enter fullscreen mode Exit fullscreen mode

This brings everyone.

SELECT * FROM students
WHERE marks > 80;
Enter fullscreen mode Exit fullscreen mode

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:

  1. Comparison Operators – compare values
  2. Logical Operators – combine conditions
  3. Range Operator – between two values
  4. Membership Operator – check from a list
  5. 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;
Enter fullscreen mode Exit fullscreen mode

Examples

SELECT * FROM students
WHERE age = 20;
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM students
WHERE marks > 75;
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM employees
WHERE salary <= 30000;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Student must be smart AND young.

OR – Any One Condition Is Enough

SELECT * FROM students
WHERE city = 'Delhi' OR city = 'Mumbai';
Enter fullscreen mode Exit fullscreen mode

Either Delhi or Mumbai. Chill.

NOT – The Opposite Game

SELECT * FROM students
WHERE NOT city = 'Delhi';
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Example

SELECT * FROM students
WHERE marks BETWEEN 60 AND 80;
Enter fullscreen mode Exit fullscreen mode

Marks 60, 61, 62 … 80 all included.

Equivalent to:

WHERE marks >= 60 AND marks <= 80;
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

Use IN like a sane developer.

IN – Value Exists in a List

SELECT * FROM students
WHERE city IN ('Delhi', 'Mumbai', 'Pune');
Enter fullscreen mode Exit fullscreen mode

NOT IN – Value NOT in the List

SELECT * FROM students
WHERE city NOT IN ('Delhi', 'Mumbai');
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Examples (Read Slowly)

Starts with 'A'

SELECT * FROM students
WHERE name LIKE 'A%';
Enter fullscreen mode Exit fullscreen mode

Aanya, Aman, Akash

Ends with 'a'

SELECT * FROM students
WHERE name LIKE '%a';
Enter fullscreen mode Exit fullscreen mode

Riya, Sanya, Neha

Contains 'an'

SELECT * FROM students
WHERE name LIKE '%an%';
Enter fullscreen mode Exit fullscreen mode

Ananya, Sandeep

Exactly 4 Letters

SELECT * FROM students
WHERE name LIKE '____';
Enter fullscreen mode Exit fullscreen mode

Four underscores = four characters.

This is where most people mess up β€” not conceptually, but syntactically.


Final Mental Cheat Sheet

SELECT * FROM table
WHERE condition;
Enter fullscreen mode Exit fullscreen mode
  • 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)