What is GREATEST Function in SQL
SQL greatest function will give the greatest string.
Description
This function returns the highest value in a list of expressions.
Syntax: greatest (strng1, string2, string3 … stringn)
Parameters
strng1
The first expression to be evaluated whether it is the greatest.
string2… stringn
Optional. Additional expressions that are to be evaluated.
Ex:
select greatest('a', 'b', 'c'), greatest('satish','srinu','saketh') from dual;
output will be
greatest('a', 'b', 'c')
------------------------
c
greatest('satish','srinu','saketh')
--------------------------
srinu
Here, this function work based on ASCII value.
output on tool
Some more Example
Let’s look at some of this function examples and explore how to use in the Oracle SQL
greatest(10,12,50,3,5)
----------------------------------
50
GREATEST('apples', 'oranges', 'bananas')
-----------------------------------------
oranges
Note:-
- In this If all parameters are null then it will display nothing. or If any parameters is null it will display nothing .
- If the datatypes of the expressions are different, all expressions will be converted to whatever datatype strng1 is before comparing.
- If the comparison is based on a character comparison, one character is considered greater than another if it has a higher character set value.
Used in
- This function can be used in the following versions of Oracle: Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i
Advantage:
This function is used to find the greatest expression out of the given input expressions. This expression might be any numbers or alphabets.