You can use the greatest()
function in Oracle to find the maximum value in the list. Below are its syntax and examples:
Syntax
greatest(value1, value2, value3, ...)
Example-1:
The following SQL query will get the maximum value from the comma-separated list:
select greatest(5, 29, 99, 89) grt from dual;
Output:
99
Example-2:
Get the maximum value in string type data:
select greatest('a', 'k', 'j', 'b') grt from dual;
Output:
k
Example-3:
Getting the column that has the maximum value in Oracle:
select ename, sal, comm, greatest(nvl(sal,0), nvl(comm,0)) highest_amt from emp;
Output
ENAME | SAL | COMM | HIGHEST_AMT |
---|---|---|---|
KING | 5000 | - | 5000 |
BLAKE | 2850 | - | 2850 |
CLARK | 2450 | 4989 | 4989 |
JONES | 2975 | - | 2975 |