MySQL
SQL
Spaceship Operator
Null-safe Comparison
Database Operators

What is this operator in MySQL?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In MySQL, the operator <=> is the null-safe equality operator. It behaves like = for ordinary non-null values, but unlike normal SQL equality it treats two NULL values as equal instead of producing an unknown result.

Why Normal Equality Is Not Enough

In SQL, NULL does not mean "an empty value." It means "unknown" or "missing." Because of that, ordinary equality does not behave the way many programmers first expect.

sql
SELECT NULL = NULL;

That does not return true. In MySQL it evaluates to NULL, which does not pass a normal WHERE filter.

The null-safe operator changes that rule:

sql
SELECT NULL <=> NULL;

This returns 1, meaning true.

That is the entire reason the operator exists: equality that continues to behave sensibly when nullable values are involved.

How <=> Behaves

Some quick examples make the behavior clear:

sql
1SELECT 5 <=> 5;       -- 1
2SELECT 5 <=> 7;       -- 0
3SELECT 5 <=> NULL;    -- 0
4SELECT NULL <=> NULL; -- 1

You can think of it as:

  • normal equality for non-null values
  • a special case where NULL equals NULL

That makes it similar in spirit to the standard SQL idea of "not distinct," even though the syntax is MySQL-specific.

Useful Query Patterns

The operator is handy when a comparison target might itself be NULL.

Suppose a table has an optional column:

sql
SELECT *
FROM users
WHERE middle_name <=> 'Anne';

That behaves much like ordinary equality.

But if the desired comparison value is NULL, the same operator still works:

sql
SELECT *
FROM users
WHERE middle_name <=> NULL;

This matches rows whose middle_name is actually NULL. With ordinary = NULL, it would not.

It is also useful in joins:

sql
1SELECT a.id, b.id
2FROM a
3JOIN b
4  ON a.optional_code <=> b.optional_code;

If both sides contain NULL, the join condition can still succeed. With =, those rows would not match on that predicate.

When to Use IS NULL Instead

<=> NULL works, but that does not mean it is always the clearest syntax. If the only question is whether a value is null, IS NULL is often more readable.

sql
SELECT *
FROM users
WHERE deleted_at IS NULL;

That is clearer than using <=> NULL for a pure null check.

So a good rule is:

  • use IS NULL or IS NOT NULL when checking nullness directly
  • use <=> when you need equality semantics that remain safe when either side may be NULL

Do Not Confuse It with Other Languages

Some languages use similar-looking operators for three-way comparison and call them spaceship operators. In MySQL, <=> is not an ordering operator. It is specifically null-safe equality.

So if you see <=> in MySQL, read it as:

text
"compare these two values for equality, but treat NULL on both sides as equal"

Common Pitfalls

  • Assuming <=> and = are interchangeable. They differ exactly when NULL appears.
  • Using = NULL and expecting it to match null values.
  • Replacing every IS NULL check with <=> NULL even when simple null predicates are clearer.
  • Importing assumptions from other languages where a similar symbol means something completely different.

Summary

  • '<=> in MySQL is the null-safe equality operator.'
  • It behaves like = for ordinary values and treats NULL <=> NULL as true.
  • It is useful for nullable comparisons in filters and joins.
  • 'IS NULL is still the clearer choice for plain null checks.'
  • Do not confuse MySQL's <=> with three-way comparison operators from other languages.

Course illustration
Course illustration

All Rights Reserved.