Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calculation of nth highest value in a Group

J2EE_LifeJan 5 2012 — edited Jan 5 2012
Hi All,

I have following problem to be solved. I have a table with following structure:
Table emp
EmpID number
NAme varchar2
DeptID number
Salary number
Now for finding max salary in each dept, I can use:
select DeptId,max(salary) from emp group by DeptId;
NOw to find second max salary, I can do below:
select DeptId,max(salary) from emp
where (DeptId,salary) not in (select DeptId,max(salary) salary from emp group by DeptId) group by DeptId;
But suppose I need to find 5th or 6th max salary then this method will be cumbersome.
I need a SQL query in which I can dynamically pass the parameter to calculate it.

Kindly help.
This post has been answered by MichaelS on Jan 5 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2012
Added on Jan 5 2012
5 comments
2,249 views