greatest(...) — Largest of the given values
Last updated: 2026-11-04
greatest(...) — Largest of the given values
greatest() returns the largest of its arguments and least() the smallest. All arguments must be mutually comparable, and the usual job is clamping values that come from several sources. Note that PostgreSQL ignores NULL arguments here, returning NULL only when every argument is NULL.
| Kind | function |
|---|---|
| Example Language | SQL |
📝 Syntax
greatest(value1 anycompatible, value2 anycompatible [, ...]) → anycompatible
least(value1 anycompatible, value2 anycompatible [, ...]) → anycompatible
⚙️ Parameters
| value anycompatible | The values to compare; their types must be mutually compatible. NULL arguments are ignored. |
|---|
Returns:anycompatible — the largest value (least() returns the smallest); the call errors if the argument types are incompatible.
▶ 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 | label | best | worst
----+----------+------+-------
1 | all set | 92 | 79
2 | one null | 70 | 70
3 | all null | |
(3 rows)
📌 Key Points
- NULL arguments are ignored: greatest(1, NULL, 3) returns 3, not NULL.
- The result is NULL only when every argument is NULL.
- Argument types must be compatible; mixing integer and text raises an error.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QHow is greatest() different from coalesce()?
Acoalesce() returns the first non-NULL value in order without comparing anything, while greatest() picks the largest among the non-NULL values.