Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

how can retrieve the maximum salary from the table

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: how can retrieve the maximum salary from the table
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: 680
Location: NJ

PostPosted: Wed Aug 02, 2006 10:35 pm    Post subject: DB2 Query
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    Post subject: Re: how can retrieve the maximum salary from the table
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    Post subject: Re: how can retrieve the maximum salary from the table
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Loading data to table gives wrong for... Raghu navaikulam DB2 16 Thu Jul 13, 2017 2:11 pm
No new posts unload data from table with lob columns farhad_evan DB2 1 Sat Apr 22, 2017 1:32 pm
No new posts Data replication from multiple Db2 ta... kishpra DB2 9 Mon Mar 27, 2017 9:58 pm
No new posts how to send just 10 rows in a CICS sc... Megha Gupta CICS 5 Thu Feb 23, 2017 6:57 pm
No new posts Join of more than one table dibyendumandal11111 DB2 6 Mon Feb 13, 2017 11:36 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us