Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 49

LIKE, BETWEEN and IN Operators

Choose the Right Operator

LIKE matches a text pattern, BETWEEN checks an inclusive range and IN tests membership in a list. They are readable alternatives to long combinations of comparison operators.

Common learning data: students contains Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and Sana (X-B, 88.50, Active).

LIKE and Wildcards

SELECT full_name FROM students
WHERE full_name LIKE 'M%';

SELECT full_name FROM students
WHERE full_name LIKE '_a%';
Results 'M%' → Meera '_a%' → Aarav, Kabir, Sana

% matches zero or more characters; _ matches exactly one. NOT LIKE reverses the pattern for known values. Case and accent sensitivity depend on collation, not on LIKE alone.

Literal Wildcards, Collation and Performance

If the searched text contains a real percent or underscore, escape it rather than letting it act as a wildcard. Define and test the escape behavior used by your client and SQL mode. A leading wildcard such as '%ana%' usually cannot use the left edge of a normal B-tree index efficiently.

Security: a wildcard is not SQL injection, but user input must still be bound through a prepared statement. Decide whether users are allowed to supply wildcards or whether the application should escape them.

BETWEEN: Inclusive Ranges

SELECT full_name, marks FROM students
WHERE marks BETWEEN 80 AND 90
ORDER BY marks;
Aarav | 86.50 Sana | 88.50

It is equivalent to marks >= 80 AND marks <= 90 for ordinary values. For timestamps, prefer a half-open range so the next period boundary is unambiguous:

WHERE created_at >= '2026-08-01'
  AND created_at <  '2026-09-01'

IN Lists, NOT IN and NULL

SELECT full_name, class_name FROM students
WHERE class_name IN ('X-A', 'XI-A')
ORDER BY student_id;
Aarav | X-A Meera | X-A

IN is equivalent to equality against any listed non-NULL value. For a subquery, NOT IN needs special care: if the subquery returns NULL, intended non-matches may become UNKNOWN. Remove NULL explicitly or express the anti-match with a correctly correlated NOT EXISTS.

Mistakes and Practice

  • Assuming LIKE is always case-sensitive.
  • Forgetting that BETWEEN includes both ends.
  • Using BETWEEN with reversed boundaries.
  • Letting NOT IN receive NULL unexpectedly.
  • Using a leading wildcard on a large frequently searched column without measuring.
  1. Find names ending in a.
  2. Return marks from 86.50 through 91.00 inclusive.
  3. Use IN for X-A and X-B, then rewrite with OR.
  4. Explain why a monthly timestamp report should use a half-open range.

Quick Summary

  • LIKE matches patterns with % and _.
  • BETWEEN includes both boundaries.
  • IN tests a list cleanly; NOT IN plus NULL needs care.
  • Collation, escaping, prepared values and indexing affect real behavior.

Official References

References reviewed 14 August 2026.

Frequently Asked Questions

What do % and _ mean in LIKE?
Percent matches zero or more characters. Underscore matches exactly one character. Their character comparison follows the expression collation.
Is BETWEEN inclusive?
Yes. BETWEEN includes both the lower and upper boundary for comparable non-NULL values.
Why use IN instead of many OR conditions?
IN expresses membership in a value list more clearly and avoids repeating the same column. The optimizer can often treat both forms similarly.
How do I search for a literal percent sign?
Escape the wildcard using the configured escape character. For portable clarity, add an explicit ESCAPE clause where supported by your chosen MySQL syntax and connection settings.
Why is NOT IN dangerous when the list contains NULL?
A NULL in the comparison list can make the predicate UNKNOWN for values that do not otherwise match. Filter NULL from the subquery or use NOT EXISTS when appropriate.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.