How to find the nth hightest record holder from a table

Showing Answers 1 - 38 of 38 Answers

Ravi S. Pasari

  • Sep 2nd, 2006
 

You can use the following e.g.1. SELECT * FROM ( SELECT deptno, ename , sal , ROW_NUMBER() OVER ( PARTITION BY deptno ORDER BY sal DESC ) Top3 FROM emp )WHERE Top3 <= 3/2. This will give distinct values.SELECT * FROM ( SELECT deptno, ename , sal , DENSE_RANK) OVER ( PARTITION BY deptno ORDER BY sal DESC ) Top3 FROM emp )WHERE Top3 <= 3/

chandra

  • Sep 13th, 2006
 

the query is select * from sample where rownum=(select max(rownum) from sample)

  Was this answer useful?  Yes

rashid

  • Sep 20th, 2006
 

u can use inline view

select sal from(select sal from emp order by sal desc) where rownum <5;

Santo

  • Oct 9th, 2006
 

Here is the query,

suppose i have table called T1 with 2 columns say ID,SAL then

 i want to find out 3rd highest salary

select a.sal from t1 a
where 3 =(select count(distinct(b.sal)) from t1 b where a.sal <= b.sal);

with Regs

Santo

Suresh

  • Oct 13th, 2006
 

select * from ( select rownum r, sal,name from (select * from emp order by sal)) where r=<n>

You need to give all column names in first inline view.

  Was this answer useful?  Yes

sankari

  • Jan 9th, 2007
 

We can use dense_rank() built in. It will rank the result set based on the column which is given in the over() clause.

For ex) to get the 7th highest salary we can use the following query.

select * from(
select empno,ename,salary,dense_rank() over(order by salary desc) r
from emp )where r=7

I have used this in 10g..i am sure about the below versions.

sudhaker

  • Jan 17th, 2007
 

to find nth highest salary u can go with this query

select min(sal) from (select distinct sal from emp order by sal desc) where rownum<='&ar';

  Was this answer useful?  Yes

diptisaxena

  • Jul 15th, 2011
 

If we have a table EMP and having data as, if we want second highest sal i.e 5000 the the query will be:

Code
  1. EMPNO ENAME SAL

  2. 1         A         5000

  3. 2         B         2000

  4. 3         C         10000

  5.  

  6. SELECT MIN(SAL) FROM (SELECT * FROM EMP ORDER BY SAL DESC)

  7. WHERE ROWNUM <=2

  8.  

Same as nth highest query we need to put that number instead of 2.

  Was this answer useful?  Yes

ravindra

  • Jul 27th, 2011
 

Code
  1. SELECT * FROM(SELECT ROW NUMBER AS rno emp.*FROM emp)WHERE RANK=&n;

  Was this answer useful?  Yes

Nihar

  • Aug 26th, 2011
 

SELECT * FROM Emp A
WHERE &n=(SELECT COUNT(DISTINCT(Sal)) FROM EMP B WHERE A.Sal<=B.Sal);

  Was this answer useful?  Yes

anitha

  • Aug 31st, 2011
 

Code
  1. SELECT * FROM emp a WHERE n = (SELECT count(DISTINCT sal) FROM emp b WHERE a.sal<=b.sal)

  2.  

  3. //IN n place mention 1, 2,3...

  4.  

  Was this answer useful?  Yes

Art11

  • Sep 27th, 2011
 

It is better to use analytic functions these days as they are faster and smarter and your managers will think high of you...

Code
  1. Top n-paid IN ALL Depts - changer the orde BY dept AND sal DESC IF ant TO find out top paid IN each dept:

  2. SELECT * FROM ( SELECT deptno, ename, sal

  3.         , ROW_NUMBER() OVER (ORDER BY sal DESC, ename) AS row_num

  4.         , RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) rank

  5.         , DENSE_RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) d_rank

  6.        FROM emp )

  7. --WHERE row_num  <= 4

  8. /

  Was this answer useful?  Yes

rohitosu

  • Mar 13th, 2012
 

This is incorrect. It will not return anything. One of the possible answer is

Code
  1. SELECT salary FROM (SELECT  rownum n, a.salary

  2. FROM

  3. (SELECT DISTINCT salary FROM person ORDER BY salary ) a)

  4. WHERE n = 5 ;

  Was this answer useful?  Yes

rohitosu

  • Mar 13th, 2012
 

This is a nice solution but it will not work correctly if any of the row has null values in it.

This is what I did. I specified n = 1 which means I want the highest salary. then I put the NULL values for this record in salary column. The record with NULL value always shows me as the highest salaried employee which is incorrect.

  Was this answer useful?  Yes

Neeraj Siddhey

  • Mar 20th, 2012
 

This will work:-

Code
  1. SELECT DISTINCT a.salary FROM emp a, emp b

  2. WHERE 9=(SELECT DISTINCT count(b.salary) FROM emp b WHERE a.salary<=b.salary);

  Was this answer useful?  Yes

manisha

  • May 27th, 2012
 

Use

Code
  1. NVL(a.sal,0)<= NVL(b.sal,0)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions