Is there any predefined function or method available to get the second highest salary from an employee table?
5 Answers
The way to do this is with Oracle's Analytic functions. Your particular scenario is just a variant on the solution I provided in another thread.
If you are interested in just selecting the second highest salary then any of DENSE_RANK(), RANK() and ROW_NUMBER() will do the trick:
SQL> select * from
2 ( select sal
3 , rank() over (order by sal desc) as rnk
4 from
5 ( select distinct sal
6 from emp )
7 )
8 where rnk = 2
9 /
SAL RNK
---------- ----------
3000 2
SQL>
However, if you want to select additional information, such as the name of the employee with the second highest salary, the function you choose will affect the result. The main reason for choosing one over another is what happens when there is a tie.
If you use ROW_NUMBER() it will return the second employee ordered by salary: what if there are two employees tying for the highest salary? What if there are two employees tying for the second highest salary? Wheareas if you use RANK() and there are two employees tying for first highest salary, there will be no records with RANK = 2.
I suggest DENSE_RANK() is the usually the safest function to choose in these cases, but it really does depend on the specific business requirement.
In Oracle you would use a query such as this one to return paged results (rows M to N):
SELECT NAME, salary
FROM (SELECT NAME, salary, ROWNUM r
FROM (SELECT NAME, salary
FROM employee
ORDER BY salary DESC
)
WHERE ROWNUM <= :N
)
WHERE r >= :M
Alternatively, you can use analytics:
SELECT NAME, salary
FROM (SELECT NAME, salary, row_number() over (ORDER BY salary DESC) n
FROM employee)
WHERE n BETWEEN :M AND :N

- 66,725
- 9
- 119
- 171
If you want to find the n highest salaries from the table, you can use this:
select min(sal)from (select distinct sal from emp order by sal desc) where rownum<=n;
where n is 1,2,3, ..., n
It is a very easy process to find out the max salary from the table.

- 2,073
- 2
- 22
- 24

- 11
- 1
Try this:
SELECT * FROM employee emp WHERE 2=(SELECT COUNT(*) FROM employee WHERE
salary >= emp.salary);

- 71,966
- 47
- 171
- 241
-
-1 I'm not sure what this will return but probably not what the question is about :-) – Aaron Digulla Mar 22 '10 at 09:53
-
It depends on what he meant with "method". For me a method is a process to obtain a result; and this is my suggested method.Thanks for my first downvote :). – systempuntoout Mar 22 '10 at 09:57
-
3@Aaron: Perhaps not the most effective way to return the result (since it uses a self-join) but it will return the correct answer, *providing salary is unique* (it won't work for example if there is three people with the same highest salary). – Vincent Malgrat Mar 22 '10 at 10:10
It's easier handling Top-N
queries with Oracle DB 12c
which introduced such a syntax :
[ OFFSET offset { ROW | ROWS } ]
[ FETCH { FIRST | NEXT } [ { rowcount | percent PERCENT } ]
{ ROW | ROWS } { ONLY | WITH TIES } ]
to be used in a query after ORDER BY
list.
In this case consider using :
select *
from emp
order by sal desc
offset 1 rows fetch next 1 rows only
where using offset
is optional, as already seen from the syntax, and points out the starting point for the fetch
clause will be offset + 1
.
A special case is having ties
which means multiple rows match the value of the Nth row(here we need 2nd row). If there are more than one people with the same salary for the top-second position, then we need to replace only
keyword by with ties
to return them all :
select *
from emp
order by sal desc
offset 1 rows fetch next 1 rows with ties

- 59,113
- 10
- 31
- 55