What is this operator in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
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:
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:
You can think of it as:
- normal equality for non-null values
- a special case where
NULLequalsNULL
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:
That behaves much like ordinary equality.
But if the desired comparison value is NULL, the same operator still works:
This matches rows whose middle_name is actually NULL. With ordinary = NULL, it would not.
It is also useful in joins:
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.
That is clearer than using <=> NULL for a pure null check.
So a good rule is:
- use
IS NULLorIS NOT NULLwhen checking nullness directly - use
<=>when you need equality semantics that remain safe when either side may beNULL
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:
Common Pitfalls
- Assuming
<=>and=are interchangeable. They differ exactly whenNULLappears. - Using
= NULLand expecting it to match null values. - Replacing every
IS NULLcheck with<=> NULLeven 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 treatsNULL <=> NULLas true. - It is useful for nullable comparisons in filters and joins.
- '
IS NULLis still the clearer choice for plain null checks.' - Do not confuse MySQL's
<=>with three-way comparison operators from other languages.
Related reading
- What Java ORM do you prefer, and why?
- What kind of database in regards to cap theorem should be used for an mmo?
- What MySQL data type should be used for Latitude/Longitude with 8 decimal places?
- What node does Cassandra store data on?
- What NoSQL DB to use for sparse Time Series like data?
- What problem does the redis distributed lock solve?
- What resources exist for Database performance-tuning?
- What scalability problems have you encountered using a NoSQL data store?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.