name salary
----- -----
mohan 500
ram 1000
dinesh 5000
hareesh 6000
mallu 7500
manju 7500
praveen 10000
hari 10000
How would I find the nth-highest salary from the aforementioned table using Oracle?
name salary
----- -----
mohan 500
ram 1000
dinesh 5000
hareesh 6000
mallu 7500
manju 7500
praveen 10000
hari 10000
How would I find the nth-highest salary from the aforementioned table using Oracle?
select distinct salary from emp_table order by salary desc limit n-1,1;
Try out following in Oracle:
Cheers!
In MySql, run the below SQL to find nth highest salary:
e.g Find 3rd highest salary:
e.g Find 3rd lowest salary (make "order by asc"):
It pretty much works in Oracle. You can use
ROW_NUMBER()
function insteadDENSE_RANK
but it selects only one record or row for the highest salary even if there are two or more employees having the equal salary.This article talks about this question in depth, and I will quote code from it below: (Note: see the bottom 2 solutions for Oracle)
Solution 1: This SQL to find the Nth highest salary should work in SQL Server, MySQL, DB2, Oracle, Teradata, and almost any other RDBMS: (note: low performance because of subquery)
The most important thing to understand in the query above is that the subquery is evaluated each and every time a row is processed by the outer query. In other words, the inner query can not be processed independently of the outer query since the inner query uses the Emp1 value as well.
In order to find the Nth highest salary, we just find the salary that has exactly N-1 salaries greater than itself.
Solution 2: Find the nth highest salary using the TOP keyword in SQL Server
Solution 3: Find the nth highest salary in SQL Server without using TOP
Note that I haven’t personally tested the SQL above, and I believe that it will only work in SQL Server 2012 and up.
Solution 4: Works in MySQL
The LIMIT clause takes two arguments in that query – the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
Solution 5: Works in Oracle
Solution 6: Works in Oracle way 2