Skip to content

GREATEST and LEAST

Return the largest or smallest value from a list of expressions. The expressions must have compatible data types.

GREATEST | LEAST ( expression [, …] )

For example:

premdb=# select greatest(1,2,3) from sys.const;
 greatest 
----------
       3
(1 row)

premdb=# select least(1,2,3) from sys.const;
 least 
-------
    1
(1 row)

premdb=# select distinct least(current_date,matchday) from match order by 1 limit 5;
       least        
---------------------
 1992-08-01 00:00:00
 1993-08-14 00:00:00
 1993-08-15 00:00:00
 1993-08-16 00:00:00
 1993-08-17 00:00:00
(5 rows)

If all expressions are null, the output is null. Otherwise, null values are ignored.

When the expressions are DECIMAL values:

  • The output will be the largest (p-s) of the inputs and the largest s of the inputs.
  • If (p-s) of the output is >38, the scale is reduced until (p-s) is <=38.

Parent topic:Conditional Expressions