Showing posts with label select nth salary from table. Show all posts
Showing posts with label select nth salary from table. Show all posts

Monday, January 17, 2011

Query for selecting second maximum salary from a table

Many a times i heard that it is the most asking questions by the mainframe opportunity seeker that "Query for selecting second maximum salary from a table"
here is the code

SELECT MAX(SAL) FROM EMP WHERE SAL < (SELECT MAX(SAL) FROM EMP);
Where SAL is the salary column,
EMP is the name of table.
___________________________________________________________________________
Find out Nth highest salary from emp table

SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);

lets take an example:
EMP
e_no e_name sal
-------------------------
001 ram 15000
002 shyam 35000
003 hari 5000
004 krishna 20000
005 rahul 25000



eg:-
a= 3;
20000
hope this will help