greatest() function

The greatest() function returns the largest of input values.

You must specify at least two input values; you can specify a maximum of four values. Variable-length lists are not supported. The comparison for string values is based on a character set value. The character with the higher character set value is considered the greatest value.

Syntax

The syntax of the function has three forms, depending on the data type of the values that you are comparing:
int4 = Greatest(int4 value1, int4 value2, ...);
int8 = Greatest(int8 value1, int8 value2, ...);
double = Greatest(double value1, double value2, ...);
date  = Greatest(date value1, date value2, ...);
varchar = Greatest(varchar value1, varchar value2, ...);
timestamp  = Greatest(timestamp value1, timestamp value2, ...);
time  = Greatest(time value1, time value2, ...);
timetz  = Greatest(timetz value1, timetz value2, ...);
interval  = Greatest(interval value1, interval value2, ...);

The value1 value specifies the first input to compare.

The value2 value specifies the second input to compare.

The value3 value specifies the third input to compare.

The value4 value specifies the fourth input to compare.

Returns

The function returns the largest value.

Example

select greatest(12,45,85);
 greatest 
----------
       85
(1 row)

select greatest(to_date('26-01-1234','DD-MM-YYYY'), 
to_date('27 Feb 1200','DD Mon YYYY'));
  GREATEST
------------
 1234-01-26
(1 row)

select greatest('Alpha Beta gamma','a');
 GREATEST
----------
 a
(1 row)

select greatest(timestamp('25-10-1756 09:00:30'), 
timestamp('24-11-1755 08:45:00'));
      GREATEST
---------------------
 1756-10-25 09:00:30
(1 row)