GREATEST()
Returns the largest value from a list of expressions.
Description
GREATEST returns the highest value among its arguments — a row-level "max
across columns" (unlike the MAX aggregate, which is across rows). GREATEST(3, 7, 5) is 7. It's perfect for picking the later of two dates or the higher of
several scores.
Syntax
GREATEST(expr1, expr2, ...)Parameters
| Name | Description | Optional |
|---|---|---|
| expr1, expr2, ... | Two or more expressions of compatible types. | No |
Return Type
Returns the same type as the inputs.
Examples
Employees & Departments
Loading database engine...
Employees & Departments
Loading database engine...
GREATEST vs MAX
GREATEST compares values within a single row (across columns); MAX is an
aggregate that compares rows within a group.
Common Mistakes
- NULL handling. In many engines a single NULL argument makes the result
NULL; use
COALESCEon inputs if you want to ignore NULLs. - Mixing types. Arguments should be compatible types; mixing number and text can error.
- Confusing with MAX.
GREATESTis per-row, not per-group.
Related Functions
See also: LEAST, MAX.