GREATEST

GREATEST is a function in SQL that returns the largest of the provided values. It compares each value and selects the highest one. If all values are null, it returns null.

GREATEST(value1, value2, …, valueN)

  • value1: This is the first value of the comparison in the `GREATEST` function. It can be any valid SQL literal or column value.
  • value2: This is the second comparative value in the `GREATEST` function. Like `value1`, it can also be any valid SQL literal, or column value.
  • …, valuen: This denotes that the GREATEST function can take any number of arguments. Each of these values (`value3`, `value4`, `valueN`, etc.) are additional comparative values, and can also be any valid SQL literal, or column value.

Example

SELECT GREATEST(3, 27, 14, 9, 5);

Output

27

Explanation

The GREATEST function in MySQL returns the greatest value from the list of parameters. The above example compares five numbers: 3, 27, 14, 9, and 5. The largest of these numbers is 27, and therefore it’s the returned output.

GREATEST(value1, value2, …, valueN)

  • value1: This is the first value to be compared. It can be a numerical value, a string, a column name, or a calculated value.
  • value2: This is the second value to be compared. Similar to value1, it can also be a numerical value, a string, a column name, or a calculated value.
  • valuen: This parameter represents any number of additional values that can be compared. The function will return the greatest value out of all the values provided. Each can be a number, a string, a column from a table, or a result of some expression.

Example

SELECT GREATEST(3, 9, 5, 7, 2);

Output

9

Explanation

The GREATEST function in PostgreSQL returns the largest of a set of provided values. In this case, it returns 9 as it is the largest number among all the input values of 3, 9, 5, 7, 2.

GREATEST( expression1 { ,…n } )

  • expression1: The first value to be compared. This can be any valid SQL Server expression of any type other than text, ntext, image, cursor, or any non-comparable common language runtime (CLR) user-defined type.
  • { ,…n }: Optional. It represents additional expressions (up to N expressions) to be compared with expression1. They must all be of the same type, or convertible to the same type, as expression1.

Example

SELECT GREATEST(10, 20, 30, 40, 50);

Output

50

Explanation

The GREATEST function returns the highest value in the list of expressions, in this case it returns 50 which is the highest value amongst 10, 20, 30, 40, 50.

GREATEST(value1, value2, …, valueN)

  • value1: This represents the first expression or value to be compared. It could be a constant value, variable, or column of a table and could be of any data type that can be implicitly converted to VARCHAR2 or NUMBER.
  • value2: This parameter indicates the second value that will be compared to the first. Similar to value1, it could be a constant value, variable, or column of a table. Its data type should be compatible with value1.
  • …, valuen: This depicts that the GREATEST function can take two or more parameters. Each additional parameter, value3, value4, and so forth up to valueN, undergoes the same comparison process. The values can be of any data type as long as they are implicitly convertible to the same data type.

Example

SELECT GREATEST(3, 40, 1, 30, 20) FROM dual;

Output

40

Explanation

The GREATEST function in Oracle, in the given example, is used to return the greatest value from the list of passed values. The list contained values 3, 40, 1, 30, and 20. From these, 40 was the greatest, which is the output.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.