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

WHERE Clause and Operators

How WHERE Filters Rows

WHERE evaluates a condition for candidate rows and keeps only rows for which the result is TRUE. FALSE and UNKNOWN do not pass the filter. UNKNOWN matters because comparisons involving NULL commonly produce it.

Learning data: students are 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).
SELECT student_id, full_name, marks
FROM students
WHERE marks >= 88
ORDER BY marks DESC;
Result student_id | full_name | marks 2 | Meera | 91.00 4 | Sana | 88.50

The condition is applied to every candidate row semantically. MySQL may use an index to avoid reading all rows, but the returned result must follow the same logic.

Comparison Operators

OperatorMeaningExample
=Equalstatus = 'Active'
<> or !=Not equalclass_name <> 'X-A'
>, >=Greater than, at leastmarks >= 80
<, <=Less than, at mostmarks < 75
<=>MySQL NULL-safe equalitymarks <=> NULL

Use normal = for known values and IS NULL for readable NULL checks. MySQL's <=> is useful in advanced comparisons where two NULL values should count as equal, but it is product-specific.

SELECT full_name
FROM students
WHERE class_name <> 'X-A'
ORDER BY student_id;
Result full_name Kabir Sana

Combine Conditions with AND, OR and NOT

SELECT full_name, marks
FROM students
WHERE status = 'Active' AND marks >= 88
ORDER BY marks DESC;
Result full_name | marks Meera | 91.00 Sana | 88.50

AND requires both conditions. OR requires at least one. NOT reverses a condition. Because AND has higher precedence than OR, always use parentheses when business logic mixes them:

SELECT full_name, class_name, status
FROM students
WHERE (class_name = 'X-A' OR class_name = 'X-B')
  AND status = 'Active'
ORDER BY student_id;
Result Aarav | X-A | Active Meera | X-A | Active Sana | X-B | Active
Dangerous ambiguity: class_name = 'X-A' OR class_name = 'X-B' AND status = 'Active' treats the AND part as one group. It can include inactive X-A rows. Parentheses make the real rule unambiguous.

BETWEEN, IN and LIKE

Inclusive range: BETWEEN

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

BETWEEN includes both boundaries. If lower and upper values are reversed, no ordinary numeric value matches.

Set membership: IN

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

IN is clearer than repeating the same column with several OR conditions.

Text pattern: LIKE

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

In LIKE patterns, % matches zero or more characters and _ matches exactly one character. Case and accent behavior depends on collation. A leading wildcard such as '%era' often prevents efficient use of a normal B-tree index.

NULL and Reliable Date Filters

NULL means no known value. Since marks = NULL is not TRUE, use:

SELECT student_id, full_name
FROM students
WHERE marks IS NULL;

SELECT student_id, full_name
FROM students
WHERE marks IS NOT NULL;

For timestamps, a half-open interval is usually safer than applying a function to the column or guessing the final fractional second:

SELECT student_id, full_name, created_at
FROM students
WHERE created_at >= '2026-08-01'
  AND created_at <  '2026-09-01'
ORDER BY created_at;

This includes all of August and excludes 1 September. It also leaves the indexed column unwrapped, which can make index use easier. Interpret timestamps according to the application's time-zone policy.

Safety, Parameters and Performance

  • Preview before changing data: run a SELECT with the exact WHERE condition before UPDATE or DELETE.
  • Bind untrusted values: use prepared statements instead of concatenating user input.
  • Match data types: compare numeric columns with numbers and date columns with valid date values.
  • Avoid unnecessary functions on indexed columns: rewrite a date filter as a range when semantics allow.
  • Index measured access paths: high-selectivity, frequently filtered columns may benefit, but every index costs storage and write work.
  • Verify with EXPLAIN: do not guess whether an index is used.
SELECT student_id, full_name
FROM students
WHERE class_name = ? AND status = ?;

The question marks are placeholders for a prepared statement. The driver sends bound values separately from SQL syntax.

Practice

  1. Return active students whose marks are between 85 and 95 inclusive.
  2. Find students in X-A or XI-A using IN.
  3. Find names whose second character is a using an underscore pattern.
  4. Write a filter for rows with unknown marks.
  5. Explain the difference between NOT status = 'Active' and status IS NULL.
  6. Write an August 2026 half-open timestamp range and explain both boundaries.

Quick Summary

  • WHERE keeps only rows whose condition is TRUE.
  • Use comparison operators for known values and IS NULL for missing values.
  • AND binds more tightly than OR; parentheses make mixed logic safe.
  • BETWEEN is inclusive, IN tests membership and LIKE matches patterns.
  • Prepared parameters, preview queries and measured indexing improve safety.

Official References

References reviewed on 14 August 2026. Results assume the four-row learning dataset.

Frequently Asked Questions

What is the WHERE clause used for?
WHERE keeps only rows whose condition evaluates to TRUE. It is used in SELECT, UPDATE and DELETE, and is especially important for limiting data-changing statements.
What is the difference between = and IS NULL?
The equals operator compares known values. NULL represents unknown or missing information, so test it with IS NULL or IS NOT NULL, not = NULL.
Is BETWEEN inclusive in MySQL?
Yes. value BETWEEN low AND high includes both boundary values and is equivalent to value >= low AND value <= high for ordinary comparable values.
Does LIKE ignore case in MySQL?
That depends on the expression collation. Many default _ci collations are case-insensitive, while binary or case-sensitive collations behave differently. Do not assume behavior without checking the schema collation.
Why are parentheses important with AND and OR?
AND has higher precedence than OR. Parentheses make the intended grouping explicit, prevent logic errors and make maintenance safer.
🔗

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.