Comparison equality operator =
Last updated: 2026-11-04
Comparison equality operator =
The equality operator = compares two operands for equality and is the most common filter in SQL. It compares integers, text, dates and even jsonb. Its most easily missed trait is three-valued logic: when either side is NULL the comparison is neither true nor false but NULL (unknown), and WHERE treats such rows as not matching and drops them.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 Syntax
a = b
Precedence: comparison operators (`< > = <= >= <>`) bind tighter than `IS NULL`/`IS TRUE`, which bind tighter than `NOT`, then `AND`, then `OR`. So `a = b AND c = d` evaluates both comparisons before AND, and `NOT a = b` parses as NOT (a = b). Comparison operators do not chain: `1 < 2 = true` is a syntax error.
⚙️ Parameters
| a | Left operand; must be comparable with b. |
|---|---|
| b | Right operand; must be comparable with a. |
Returns:The boolean true, false, or NULL when either operand is NULL.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT id, a = b AS eq_result FROM t_eq ORDER BY id
id | eq_result
---+----------
1 | true
2 | false
3 | NULL
4 | NULL
(4 rows)
SELECT id, a IS NOT DISTINCT FROM b AS not_distinct_result FROM t_eq WHERE id IN (3, 4) ORDER BY id
id | not_distinct_result
---+--------------------
3 | true
4 | false
(2 rows)
📌 Key Points
- A comparison involving NULL returns NULL rather than false, and WHERE keeps only rows whose result is true.
- `NULL = NULL` is NULL, so it cannot tell you whether two NULLs are the same.
- Use `IS NOT DISTINCT FROM` to treat NULLs as equal and `IS DISTINCT FROM` to treat them as different.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy does WHERE a = NULL match no rows?
ABecause a = NULL evaluates to NULL (unknown) instead of true, and WHERE keeps only true rows. Use `a IS NULL` to match NULLs.