coalesce(...) — First non-NULL argument
Last updated: 2026-11-04
coalesce(...) — First non-NULL argument
coalesce() returns the first non-NULL argument from left to right, or NULL if they are all NULL. It is the most common way to deal with nullable columns, and it pairs with CASE expressions to turn missing values into meaningful business defaults.
| Kind | function |
|---|---|
| Example Language | SQL |
📝 Syntax
coalesce(value1 anycompatible, value2 anycompatible [, ...]) → anycompatible
⚙️ Parameters
| value anycompatible | The candidate values, whose types must be mutually compatible; the first non-NULL one is returned. |
|---|
Returns:anycompatible — the first non-NULL argument, or NULL when they are all 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:
id | contact | email_or_default
----+-----------------+------------------
1 | ada@example.com | ada@example.com
2 | 13800000000 | no-email
3 | ada_l | no-email
4 | unknown | no-email
(4 rows)
📌 Key Points
- Arguments are evaluated left to right and the first non-NULL one wins.
- If every argument is NULL the result is NULL — the trailing default is only reached when the earlier ones are NULL.
- The arguments must share a common type: coalesce(NULL, 0) infers integer while coalesce(NULL, 0.5) infers numeric.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QDoes coalesce() evaluate every argument like CASE does?
ANo. coalesce() is a standard SQL expression and the planner normally stops at the first non-NULL argument, so it is safe to put an expensive function last.