MySQL
MySQL

Comparison Operators

Comparison operators are how WHERE conditions actually compare values. You've already seen = and >; MySQL supports a full set. = checks equality (age = 16). != or <> checks "not equal" (age != 16). > and < check strictly greater than / less than. >= and <= check greater-than-or-equal and less-than-or-equal, which include the boundary value itself, unlike > and < alone. BETWEEN offers a clean shorthand for a range: WHERE age BETWEEN 15 AND 17; is equivalent to WHERE age >= 15 AND age <= 17;, but reads more clearly for an inclusive range. These operators work the same way whether you're comparing numbers, text, or dates though comparing text is typically alphabetical ("Ali" < "Sara" is true, because A comes before S), and comparing dates follows calendar order (earlier dates are "smaller").

Why this matters

  • ·A rich set of comparison operators lets WHERE express precise, specific conditions.
  • ·BETWEEN reads clearly for inclusive ranges, instead of writing two separate conditions.
  • ·The same operators work consistently across numbers, text, and dates.

Common mistakes

  • ·Confusing > with >= (or < with <=) silently excludes or includes a boundary value you didn't intend.
  • ·BETWEEN is inclusive on both ends forgetting that can lead to off-by-one mistakes.
  • ·Comparing values of mismatched types (like a number column to a quoted date-looking string) can behave unexpectedly.
Your codePHP
Expected output
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1  | Ali  | 16  |
+----+------+-----+
1 row in set

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

> excludes the exact boundary value; >= includes it. age > 16 excludes students exactly 16, while age >= 16 includes them.

Practice questions

Question 1

Write a query that returns students with age not equal to 16.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Write a query that returns students with age between 14 and 18 using BETWEEN.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

Which operator means 'not equal'?