MySQL comparison operators compare values and produce a Boolean-style result: true, false, or, for ordinary comparisons involving NULL, unknown. They are commonly used in a WHERE clause to decide which rows should be returned.
SELECT id,
name,
mark
FROM student
WHERE mark >= 85
ORDER BY mark DESC, id;
= and <> do not treat NULL like a normal value. Use IS NULL, IS NOT NULL, or MySQL's NULL-safe equality operator <=> when NULL has to be handled explicitly.| Operator / Predicate | Meaning |
|---|---|
= | Equal to |
<> or != | Not equal to |
<=> | MySQL NULL-safe equality |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
| IN / NOT IN | Membership in, or exclusion from, a set of values |
| BETWEEN / NOT BETWEEN | Inside or outside an inclusive range |
| IS NULL / IS NOT NULL | Test whether a value is NULL |
| IS TRUE / IS FALSE / IS UNKNOWN | Boolean-style tests |
| LIKE / NOT LIKE | Pattern matching |
IN, BETWEEN, IS NULL, and LIKE are predicates rather than simple two-value operators, but they belong to the same practical family of SQL conditions used for filtering rows.
Return students whose mark is exactly 85:
SELECT id,
name,
class,
mark
FROM student
WHERE mark = 85
ORDER BY id;
| id | name | class | mark |
|---|---|---|---|
| 2 | Max Ruin | Three | 85 |
| 8 | Asruid | Five | 85 |
For text values, quote the string:
SELECT id,
name,
class
FROM student
WHERE class = 'Six';
MySQL accepts both <> and != for ordinary not-equal comparison.
SELECT id,
name,
class
FROM student
WHERE class <> 'Six';
The equivalent MySQL form is:
SELECT id,
name,
class
FROM student
WHERE class != 'Six';
class is NULL are not returned by either expression, because NULL <> 'Six' is unknown rather than true. Include NULL explicitly if your requirement says it should count as "not Six."SELECT id,
name,
class
FROM student
WHERE class <> 'Six'
OR class IS NULL;
Marks above 50:
SELECT id, name, mark
FROM student
WHERE mark > 50;
Marks of 85 or more:
SELECT id, name, mark
FROM student
WHERE mark >= 85;
The boundary value is excluded by > and included by >=.
Marks below 25:
SELECT id, name, mark
FROM student
WHERE mark < 25;
Marks of 25 or below:
SELECT id, name, mark
FROM student
WHERE mark <= 25;
MySQL's <=> operator compares two values like equality but handles NULL explicitly.
Consider this table:
| id | first_name | last_name |
|---|---|---|
| 2 | Alex | John |
| 3 | Ron | Ron |
| 4 | NULL | NULL |
Ordinary equality:
SELECT id,
first_name,
last_name
FROM table1
WHERE first_name = last_name;
returns the row where both strings are Ron, but not the row where both values are NULL.
| id | first_name | last_name |
|---|---|---|
| 3 | Ron | Ron |
Why? Because:
SELECT NULL = NULL AS ordinary_equality;
returns NULL, not true.
Now use NULL-safe equality:
SELECT id,
first_name,
last_name
FROM table1
WHERE first_name <=> last_name;
| id | first_name | last_name |
|---|---|---|
| 3 | Ron | Ron |
| 4 | NULL | NULL |
With <=>, two NULL values are considered equal for this comparison.
MySQL does not have a separate NULL-safe not-equal symbol. Negate the NULL-safe equality test:
SELECT id,
first_name,
last_name
FROM table1
WHERE NOT (first_name <=> last_name);
See SQL NULL values.
To test whether one expression is NULL, use IS NULL:
SELECT id,
first_name,
last_name
FROM table1
WHERE first_name IS NULL;
To return non-NULL values:
SELECT id,
first_name,
last_name
FROM table1
WHERE first_name IS NOT NULL;
first_name = NULL or first_name != NULL. Ordinary equality and inequality do not test NULL the way beginners often expect.Use IN when a value may match any member of a set:
SELECT id,
name,
class
FROM student
WHERE class IN (
'Four',
'Five'
);
Use NOT IN to exclude members of the set:
SELECT id,
name,
class
FROM student
WHERE class NOT IN (
'Four',
'Five'
);
NOT EXISTS when appropriate.BETWEEN includes both boundary values:
SELECT id,
name,
mark
FROM student
WHERE mark BETWEEN 50 AND 60;
This is equivalent to:
WHERE mark >= 50
AND mark <= 60
To return values outside that inclusive range:
SELECT id,
name,
mark
FROM student
WHERE mark NOT BETWEEN 50 AND 60;
MySQL supports Boolean-style predicates such as IS TRUE, IS FALSE, and IS UNKNOWN.
Using the existing plus2_boolean sample table:
SELECT *
FROM plus2_boolean
WHERE mar IS TRUE;
Using IS NOT TRUE:
SELECT *
FROM plus2_boolean
WHERE jan IS NOT TRUE;
See MySQL IS operator.
IS NOT TRUE therefore also accepts values that are false or unknown.LIKE is used for pattern matching rather than ordinary equality:
SELECT id,
name
FROM student
WHERE name LIKE 'John%';
% matches any sequence of characters. Use _ to match one character.
Text comparisons depend on the active character set and collation. A case-insensitive collation can treat differently cased strings as equal.
SELECT name
FROM student
WHERE name = 'JOHN DEO';
Whether that matches John Deo depends on the column/expression collation.
STRCMP() compares two strings according to MySQL string-comparison rules and returns:
0 when the strings compare as equal,SELECT STRCMP(
'plus2net',
'PLUS2NET'
);
Under a case-insensitive collation, these strings can compare as equal and return 0. The result is not universally guaranteed to be 0 under every possible collation.
Comparison expressions are often combined with logical operators:
SELECT id,
name,
class,
mark
FROM student
WHERE class = 'Four'
AND mark >= 60
ORDER BY mark DESC, id;
When both AND and OR appear, use parentheses to make the intended precedence obvious:
SELECT id,
name,
class,
mark
FROM student
WHERE (
class = 'Four'
OR class = 'Five'
)
AND mark >= 60;
See SQL AND / OR logical operators.
When the comparison value comes from the application, bind it rather than concatenating it into SQL:
<?php
$minimum_mark=85;
$sql="SELECT id, name, class, mark
FROM student
WHERE mark >= :minimum_mark
ORDER BY mark DESC, id";
$stmt=$dbo->prepare($sql);
$stmt->bindValue(
':minimum_mark',
$minimum_mark,
PDO::PARAM_INT
);
$stmt->execute();
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row){
echo '<p>'
.htmlspecialchars(
$row['name'],
ENT_QUOTES,
'Windows-1252'
)
.' : '
.(int)$row['mark']
.'</p>';
}
Prepared statements protect data values. The comparison operator itself should come from trusted application logic rather than arbitrary user input.
Use IS NULL, IS NOT NULL, or <=> when NULL must be handled explicitly.
column <> 'value' does not return NULL rows. Add OR column IS NULL if that is the required logic.
BETWEEN 50 AND 60 includes both 50 and 60.
NULL in a NOT IN subquery can make the result unknown. Filter NULL or use NOT EXISTS where appropriate.
Case and accent behavior depends on collation.
Its result follows MySQL string comparison/collation rules.
Bind values with prepared statements and select permitted comparison operators through trusted allowlisted application logic.
SQL dump of plus2_boolean table:
SQL dump of student3 table:
The main ordinary operators are =, <> or !=, >, >=, <, and <=. MySQL also provides the NULL-safe equality operator <=>.
= follows normal SQL NULL semantics, so NULL = NULL returns NULL. <=> is NULL-safe and returns true when both operands are NULL.
Yes. Both mean ordinary not equal. <> is the standard SQL form.
Yes. BETWEEN is inclusive at both ends.
Comparisons with NULL are unknown rather than true or false. Add OR column IS NULL if NULL rows should also be returned.
If the compared set contains NULL, the NOT IN result can become unknown. Remove NULL from the set or use NOT EXISTS when appropriate.
It depends on the character-set collation used by the column or expression.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.