IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

how to calculate the 2nd max salary in a emp table.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
iknow

Active User


Joined: 22 Aug 2005
Posts: 411
Location: Colarado, US

PostPosted: Thu Oct 13, 2005 10:05 am
Reply with quote

Hi All

I have a employee table with a salary field. How to calculate the second maximum salary in the employee table. Please provide the query.
Back to top
View user's profile Send private message
sridevi2005

New User


Joined: 15 Sep 2005
Posts: 42

PostPosted: Thu Oct 13, 2005 11:25 am
Reply with quote

Hi,

Try the following query.

Select min(salary) from employee
where salary > select salary from employee
where salary > avg(sal).

Please let me know if u have any query.

Thanks,
Sridevi
Back to top
View user's profile Send private message
mathiprakash

New User


Joined: 29 Apr 2005
Posts: 14
Location: Pune, India.

PostPosted: Thu Oct 13, 2005 12:26 pm
Reply with quote

Hi,

This topic has already been disscussed so many times.

Not only the second maximum, you can find for any Nth max of a column.

For more information,
www.ibmmainframes.com/viewtopic.php?t=1472&highlight=maximum

Regards,
Matty.
Back to top
View user's profile Send private message
radhakrishnan82

Active User


Joined: 31 Mar 2005
Posts: 435
Location: chennai, India

PostPosted: Thu Oct 13, 2005 12:30 pm
Reply with quote

try this code:

Code:
SELECT DISTINCT (SALARY)
FROM EMPTABLE A
WHERE 2 =
(SELECT COUNT (DISTINCT (B.SALARY))
FROM EMPTABLE B
WHERE A.SALARY <= B.SALARY);


Try the following to find the 'n'th max salary of the table:

Quote:
Code:
SELECT EMPSAL FROM EMPTABLE X
WHERE (n-1) IN
(SELECT COUNT(*) FROM EMPTABLE Y
WHERE X.EMPSAL > Y.EMPSAL)


Hope this helps.
Back to top
View user's profile Send private message
Kotes

New User


Joined: 03 May 2005
Posts: 5

PostPosted: Thu Oct 13, 2005 1:42 pm
Reply with quote

Hi Iknow,
What radhakrishnan wrote is sucessfully works.
Back to top
View user's profile Send private message
jeyakanthan

New User


Joined: 18 May 2005
Posts: 12
Location: chennai

PostPosted: Fri Oct 14, 2005 7:48 pm
Reply with quote

this is simple query
select max(salary) from emp-table where salary NOT IN
(select max(salary) from emp-table)
Back to top
View user's profile Send private message
nijugopalan

New User


Joined: 15 Oct 2005
Posts: 15
Location: pune

PostPosted: Thu Oct 20, 2005 8:03 pm
Reply with quote

select max sal from emp where sal<(select max sal from emp)


is this correct
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Check data with Exception Table DB2 0
No new posts how to calculate SUM value for VB fil... DFSORT/ICETOOL 1
Search our Forums:

Back to Top