Comparison functions
Comparison functions select extrema from a list of expressions. They return one of the original values, preserving its type.
greatest and least skip NULL arguments. The scalar forms of min and max return NULL if any argument is NULL; their one-argument aggregate forms instead skip NULL rows. Choose the function according to the missing-value behavior you need.
Example: Comparing optional values
SELECT greatest(10, NULL, 20), least(10, NULL, 20), max(10, NULL, 20)
FORMAT csv;20,10,\Ngreatest
Return the largest non-NULL argument using SQL value ordering and the selected collation.
Syntax
greatest(value1, ...)Arguments
One or more SQL values; NULL arguments are skipped. Comparisons follow local SQL value ordering rather than PostgreSQL implicit common-type coercion.
Returned value
Selected argument with its type preserved; NULL if every argument is NULL.
Example
SELECT greatest(1,NULL,3) FORMAT csv;3least
Return the smallest non-NULL argument using SQL value ordering and the selected collation.
Syntax
least(value1, ...)Arguments
One or more SQL values; NULL arguments are skipped. Comparisons follow local SQL value ordering rather than PostgreSQL implicit common-type coercion.
Returned value
Selected argument with its type preserved; NULL if every argument is NULL.
Example
SELECT least(1,NULL,-2) FORMAT csv;-2max
Return the largest of two or more values using SQL value ordering and collation.
Syntax
max(value1, value2, ...)Arguments
Two or more SQL values. Any NULL argument makes the result NULL; one argument selects the aggregate form.
Returned value
The selected input value with its type preserved, or NULL when any argument is NULL.
Example
SELECT max(3,1,2) FORMAT csv;3min
Return the smallest of two or more values using SQL value ordering and collation.
Syntax
min(value1, value2, ...)Arguments
Two or more SQL values. Any NULL argument makes the result NULL; one argument selects the aggregate form.
Returned value
The selected input value with its type preserved, or NULL when any argument is NULL.
Example
SELECT min(3,1,2) FORMAT csv;1