GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  SQL
Go To First  |  Previous Question  |  Next Question 
 SQL  |  Question 101 of 171    Print  
How to find Nth largest or Nth smallest data from oracle table, for ex..5th highest salary from employees

  
Total Answers and Comments: 15 Last Update: January 01, 2007     Asked by: vidhyalakshmi 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: ALBERT ASHISH
 

To find the nth higest salary:

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

To find the nth smallest salary:

select max(sal) from (select distinct sal from emp order by sal) where rownum<=&n;

Dont get confused... its min(sal) for highest and max(sal) for lowest. Becos:

Ex: Top 3 salaries - 5000,3000,2975 for 3rd higest min(sal) i.e 2975...



Above answer was rated as good by the following members:
souravbhattacharya1
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
September 21, 2006 00:42:25   #1  
faizal khan        

RE: How to find Nth largest or Nth smallest data from ...
This is one of the solutions to it. there are many more.select * from ( select last_name salary dense_rank() over (order by salary desc) rank from EMPLOYEES) where rank 10
 
Is this answer useful? Yes | No
September 25, 2006 05:21:35   #2  
suresh.kandukuru Member Since: September 2006   Contribution: 3    

RE: How to find Nth largest or Nth smallest data from ...
1 st methodselect empno from (select empno rownum rn from emp order by empno desc) it where it.rn 22 nd methodselect max(empno) from emp where level 2 connect by prior empno>empno group by level;http://360.yahoo.com/suresh.kandukuru
 
Is this answer useful? Yes | No
October 03, 2006 14:55:27   #3  
ramesh        

RE: How to find Nth largest or Nth smallest data from ...

try this one..hope it wil be easy..

select rownum sal from (select distinct sal from emp order by sal) group by rownum sal having rownum 5;


 
Is this answer useful? Yes | No
October 04, 2006 12:51:11   #4  
Mamata Kalbande        

RE: How to find Nth largest or Nth smallest data from ...

To get the nth largest unitprice from products table:

SELECT distinct p1.unitprice

from products p1

where

(select count(*)+1 from products p2 where

p2.unitprice > p1.unitprice) {n}

order by p1.unitprice desc

To get the nth smallest unitprice from products table:

SELECT distinct p1.unitprice

from products p1

where

(select count(*)+1 from products p2 where

p2.unitprice < p1.unitprice) {n}

order by p1.unitprice asc


 
Is this answer useful? Yes | No
October 13, 2006 08:57:50   #5  
Rohan Deshpande        

RE: How to find Nth largest or Nth smallest data from ...
query select * from(select ename sal from emp order by sal desc) where rownum< 5;
 
Is this answer useful? Yes | No
October 18, 2006 15:18:18   #6  
ALBERT ASHISH        

RE: How to find Nth largest or Nth smallest data from ...

To find the nth higest salary:

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

To find the nth smallest salary:

select max(sal) from (select distinct sal from emp order by sal) where rownum< &n;

Dont get confused... its min(sal) for highest and max(sal) for lowest. Becos:

Ex: Top 3 salaries - 5000 3000 2975 for 3rd higest min(sal) i.e 2975...


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
November 01, 2006 05:09:39   #7  
GC Shekar        

RE: How to find Nth largest or Nth smallest data from ...

Assume you have Table T1 with Columns C1 containing the Name and Salary in C2.
Below SQL would give the required result:

SELECT *
FROM (SELECT c2 ROWNUM rn
FROM (SELECT DISTINCT c2
FROM t1 ORDER BY c2)
)
WHERE rn <nth smallest data>

Good Luck!
Ganesh


 
Is this answer useful? Yes | No
November 02, 2006 03:56:02   #8  
Das        

RE: How to find Nth largest or Nth smallest data from ...
SOrry this does not give the desired result. It returns the n number of records in desc order
 
Is this answer useful? Yes | No
November 04, 2006 06:23:24   #9  
Syed Jawahar        

RE: How to find Nth largest or Nth smallest data from ...

hi please find the exact solution for the nth highest salary.

METHOD 1:

select empname salary from emp where sal ( select min(sal) from (select distinct sal from emp order by sal desc) where rownum< n)

n---> nth salary

METHOD 2:

SELECT ename sal from emp a where (n-1) (select count(*) from (select distinct(sal) from emp) b where b.sal>a.sal)

for eg to calculate 5th highest salary. substitute 4 in where clause(ie 5-1)

Explanation of the query:

For every row in emp. Count the number of salaries greater than that row. when you find a row with only 3 greater salaries you must have the 4th highest right there.

NOTE: please dont write statements like 'where rownum n'. It wont work at all. because rownum will be generated at runtime. so rownum< 5 is correct and rownum 5 is wrong(means it wont give any error but will not return any rows) You can write only rownum 1 but not 2 3 etc..


 
Is this answer useful? Yes | No
November 08, 2006 05:25:52   #10  
parvathi        

RE: How to find Nth largest or Nth smallest data from ...

Hi

Using Correlated Query it is possible to find 5th or nth hight salary.The Correlated Query is

select a.sal from emp a where 5 (select count(distinct(b.sal)) from emp b where a.sal< b.sal).

in place of 5 we can use nth salaray also.

Thanks

Parvathi.


 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape