GREATEST Function in Oracle SQL – PLSQL

The GREATEST function in Oracle SQL / PLSQL is used to get the greatest value out of the expressions provided.

The Syntax for the GREATEST function in Oracle SQL / PLSQL is:

SELECT GREATEST(expression1, expression2, expression3. . . ., expressionN)
FROM table_name;

Expression1, expression2, expression3 … expressionN are expressions evaluated by the greatest function.

  • If the data types of the expressions are different then all the expressions are converted to expression1 data type.
  • In character based comparison one character is considered greater than another if it has a higher character set.

Example 1:
[sourcecode language=”sql”]
SELECT GREATEST(3,6,12,3)
FROM DUAL;[/sourcecode]

Will return “12”


Example 2:
[sourcecode language=”sql”]
SELECT GREATEST(‘3′,’6′,’12’,’3′)
FROM DUAL;[/sourcecode]

Will return “6”


Example 3:
[sourcecode language=”sql”]
SELECT GREATEST(‘apples’, ‘grapes’, ‘bananas’)
FROM DUAL;[/sourcecode]

Will return “grapes”


Leave a Reply

Your email address will not be published. Required fields are marked *