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

how can retrieve the maximum salary from the table


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

New User


Joined: 02 Aug 2006
Posts: 23
Location: chennai

PostPosted: Wed Aug 02, 2006 9:39 pm
Reply with quote

hi all,


how can i retrieve maximum salary from the table and second highest saalry from the table too.
Back to top
View user's profile Send private message
khamarutheen

Active Member


Joined: 23 Aug 2005
Posts: 677
Location: NJ

PostPosted: Wed Aug 02, 2006 10:35 pm
Reply with quote

kannag,

Hope the below query will help icon_biggrin.gif



Quote:
SELECT MAX(SAL) FROM EMP WHERE < (SELECT MAX(SAL) FROM EMP ORDER BY DESC)
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Thu Aug 03, 2006 1:29 am
Reply with quote

Hi kannag,

Here are two SELECTs that will work for you.

The first will return one row with two columns, Highest and Second_highest salary, and the second will return two rows with one column, First row highest, second second_highest salary.

Code:

SELECT S2.SALARY         AS HIGHEST,         
       MAX(S1.SALARY)    AS SECOND_HIGHEST   
  FROM DB2.EMP            S1,               
       (SELECT MAX(SALARY) AS SALARY         
          FROM DB2.EMP                       
        ) S2                                 
 WHERE S1.SALARY < S2.SALARY                 
 GROUP BY S2.SALARY                         
;                                           
SELECT DISTINCT SALARY                       
  FROM DB2.EMP                               
 ORDER BY SALARY DESC                       
 FETCH FIRST 2 ROWS ONLY                     
;                                           


Dave
Back to top
View user's profile Send private message
umed

New User


Joined: 13 May 2005
Posts: 38

PostPosted: Thu Aug 03, 2006 3:09 am
Reply with quote

Try following query

Select sal from ( select sal from EMP order by sal Desc) fetch first 2 rows only

Please correct if I am wrong

Thanks,
Umed
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 Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top