Skip to content
SQLSimplified
numeric

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

NameDescriptionOptional
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 COALESCE on inputs if you want to ignore NULLs.
  • Mixing types. Arguments should be compatible types; mixing number and text can error.
  • Confusing with MAX. GREATEST is per-row, not per-group.

See also: LEAST, MAX.

Search

Search lessons, functions, examples, and practice problems