Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
Retrieve the Second highest salary of each dept

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Interview Questions
Author Message
afroz alam

New User


Joined: 02 Jul 2007
Posts: 9
Location: India

PostPosted: Thu Jul 05, 2007 5:03 pm    Post subject: Retrieve the Second highest salary of each dept
Reply with quote

Hi all.....

I have emp table . i want to retrieve second highest salary of each department.

What will be the query[/b]
Back to top
View user's profile Send private message
References
Nimesh.Srivastava

Active User


Joined: 30 Nov 2006
Posts: 76
Location: SINGAPORE

PostPosted: Thu Jul 05, 2007 7:01 pm    Post subject:
Reply with quote

afroz alam,
I think an commonly asked interview question (you are asked to do it in one query) icon_lol.gif
anyways here's the query which would do so
Code:
select empid,emp_sal
from emp
where emp_sal < (select max(emp_sal) from emp)
order by emp_sal desc
fetch first 1 rows only;


Thanks
Nimesh
Back to top
View user's profile Send private message
Nimesh.Srivastava

Active User


Joined: 30 Nov 2006
Posts: 76
Location: SINGAPORE

PostPosted: Thu Jul 05, 2007 7:18 pm    Post subject:
Reply with quote

oops didn't saw the dept part icon_redface.gif
I am not sure it can be done in a single query or not but if you can use a cursor then, it would be like this
cursor to select distinct dept- for each dept
select dept,emp_sal
from emp
where emp_sal e1 < (select max(emp_sal) from emp e2 where
e2.dept = :h_dept)
and e1.dept = :h_dept
order by emp_sal desc
fetch first 1 rows only;
print dept,salary
cursor close

Thanks
Nimesh
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Interview Questions All times are GMT + 6 Hours
Page 1 of 1