GeekInterview.com > Interview Questions > Oracle > SQL
Go To First | Previous Question | Next Question
maxvalue.sql Select the Nth Highest value from a table
select level, max('col_name') from my_table where level = '&n' connect by prior ('col_name') >
'col_name')
group by level;
Example:
Given a table called emp with the following columns:
-- id number
-- name varchar2(20)
-- sal number
--
-- For the second highest salary:
-- select level, max(sal) from emp
-- where level=2
-- connect by prior sal > sal
-- group by level
Total Answers and Comments: 17
Last Update: October 10, 2007
No best answer available. Please pick the good answer available or submit your answer.
Sorting Options
Latest First
Oldest First
By Rating
August 02, 2006 07:33:55 #5
gannesunil
Member Since: August 2006 Contribution: 6
RE: maxvalue.sql Select the Nth Highest value from a t... select * from emp where sal=(select max(sal) from emp);
regards
ganne
Is this answer useful? Yes | No
November 21, 2006 12:15:44 #8
Nitesh Gangrade
RE: maxvalue.sql Select the Nth Highest value from a t... Select * from Employee e1 where N-n = (select count(distinct(e2.salary)) from Employee e2 where e2.salary>e1.salary;
Where N = no of rows in table that we can find through
N=Select count(distinct(*)) from employee;
n = nth max record.
Is this answer useful? Yes | No
November 27, 2006 02:52:28 #10
sganne
RE: maxvalue.sql Select the Nth Highest value from a t... select * FROM (SELECT ROWNUM rn,sal from(SELECT * FROM emp ORDER BY sal DESC)) WHERE rn=&x; try likr this
regards
ganne
Is this answer useful? Yes | No
Go To Top