问题
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?
回答1:
you can use something like this.. this is what i have tested and then pasted here
SELECT *
FROM tblname
WHERE salary = (SELECT *
FROM (SELECT *
FROM (SELECT *
FROM (SELECT DISTINCT( salary )
FROM tblname
ORDER BY salary DESC) A
WHERE rownum <= nth) B
ORDER BY salary ASC) C
WHERE rownum <= 1)
in place of 'tblname' give your table name and then in place nth give your desired nth highest salary that you want

you can see in the screen shot that it is working.
回答2:
select *
from ( select s.*, rank() over (order by salary desc) as rownumber
from salary )
where rownumber = nth
pass your salary number in place of "nth"
回答3:
select * from
(
select sal, rank() over (order by sal DESC/ASC) rnk
from emp
)
where rnk = 1/2/3/4/5/6/...;
回答4:
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)
SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
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
SELECT TOP 1 Salary
FROM (
SELECT DISTINCT TOP N Salary
FROM Employee
ORDER BY Salary DESC
) AS Emp
ORDER BY Salary
Solution 3: Find the nth highest salary in SQL Server without using TOP
SELECT Salary FROM Employee
ORDER BY Salary DESC OFFSET N-1 ROW(S)
FETCH FIRST ROW ONLY
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
SELECT Salary FROM Employee
ORDER BY Salary DESC LIMIT n-1,1
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
select * from (
select Emp.*,
row_number() over (order by Salary DESC) rownumb
from Employee Emp
)
where rownumb = n; /*n is nth highest salary*/
Solution 6: Works in Oracle way 2
select * FROM (
select EmployeeID, Salary
,rank() over (order by Salary DESC) ranking
from Employee
)
WHERE ranking = N;
回答5:
General query for all Database
SELECT DISTINCT salary FROM emp X WHERE n =
( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary >=X.salary )
Replace n with the given number. for example to get the 3rd highest salary
SELECT DISTINCT salary FROM emp X WHERE 3 =
( SELECT COUNT(DISTINCT salary) FROM emp WHERE salary >=X.salary )
OR
in any programming language
select * from emp order by salary
then iterate the result set in programming language (JAVA, .net or php)
Mysql
SELECT DISTINCT salary FROM emp X order by salary desc limit n,1
回答6:
You've tagged your question Oracle so you could just use the NTH_VALUE() function... it's an analytic function unfortunately but your query would be simplified to:
select distinct nth_value(salary, 3) over ()
from employees
SQL Fiddle
From 12c Oracle finally catches up with the rest of the world and includes OFFSET so you could use this instead:
select salary
from employees
order by salary
offset n - 1
fetch next row only
回答7:
DECLARE M INT; SET M=N-1; SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M,1;
回答8:
You can find plenty of stuff on google
select *
from table_name T1
where Nth = (select count(distinct (T2.sal))
from table_name T2
where T1.sal <= T2.sal )
回答9:
Select n-th highest salary in a simple way
SELECT emp_no, sal
FROM
(
select emp_no, sal, ROW_NUMBER() OVER (order by sal desc) RN
from emp
order by sal desc
)
WHERE RN = n;
Where n = the n-th number u want.....
回答10:
Refer following query for getting nth highest salary. By this way you get nth highest salary. If you want get nth lowest salary only you need to replace DESC by ASC in the query.

回答11:
In MySql, run the below SQL to find nth highest salary:
SELECT distinct(salary), emp_id, name
FROM `emp_salary`
group by salary
order by salary desc limit N-1,1;
e.g Find 3rd highest salary:
SELECT distinct(salary), emp_id, name
FROM `emp_salary`
group by salary
order by salary desc limit 2,1;
e.g Find 3rd lowest salary (make "order by asc"):
SELECT distinct(salary), emp_id, name
FROM `emp_salary`
group by salary
order by salary asc limit 2,1;
回答12:
In Sql server 2012 and above. Please Refer this link for Fetch, Offset, Sql server Page
Use AdventureWorks /* AdventureWorks 2014 DB*/
Select distinct(CommissionPct) from Sales.SalesPerson
order by CommissionPct desc OffSet 3 Rows Fetch next 1 Rows only
--This for 4Th highest value (N-1)
Look here
回答13:
Try out following in Oracle:
SELECT *
FROM
(SELECT rownum AS rn,
a.*
FROM
(WITH DATA AS -- creating dummy data
( SELECT 'MOHAN' AS NAME, 200 AS SALARY FROM DUAL
UNION ALL
SELECT 'AKSHAY' AS NAME, 500 AS SALARY FROM DUAL
UNION ALL
SELECT 'HARI' AS NAME, 300 AS SALARY FROM DUAL
UNION ALL
SELECT 'RAM' AS NAME, 400 AS SALARY FROM DUAL
)
SELECT D.* FROM DATA D ORDER BY SALARY DESC
) A
)
WHERE rn = 3; -- specify N'th highest here (In this case fetching 3'rd highest)
Cheers!
回答14:
We can do this by correlated subquery.
SELECT Salary
FROM Employee E1
WHERE N-1 = (SELECT COUNT(*)
FROM Employee E2
WHERE E1.salary <E2.Salary)
For further knowledge please check this link.. Correlated Subquery with example
回答15:
select distinct salary from emp_table order by salary desc limit n-1,1;
回答16:
SELECT * FROM (SELECT SALARY, DENSE_RANK() OVER ( ORDER BY SALARY DESC) nth_salary FROM EMPLOYEES)WHERE nth_salary = 1;
It pretty much works in Oracle. You can use ROW_NUMBER()
function instead DENSE_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.
来源:https://stackoverflow.com/questions/18285903/find-out-the-nth-highest-salary-from-table