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

How to get 2nd highest row from a table?


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

New User


Joined: 28 May 2007
Posts: 6
Location: Hyderabad

PostPosted: Mon Jan 10, 2011 12:38 pm
Reply with quote

Let's say we have a table given below with emp name and emp salary.

How to get 2nd highest salary from the below table. Can someone provide the query?


A | 3000
B | 1500
C | 4000
D | 7000
E 2000
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Jan 10, 2011 12:48 pm
Reply with quote

Maybe you should try to search the db2 forum for second max ?
Back to top
View user's profile Send private message
kumar32149

New User


Joined: 22 Sep 2010
Posts: 9
Location: India

PostPosted: Mon Jan 10, 2011 5:24 pm
Reply with quote

select max(sal) from emp where sal<(select max(sal) from emp);
Back to top
View user's profile Send private message
manikawnth

New User


Joined: 07 Feb 2007
Posts: 61
Location: Mumbai

PostPosted: Mon Jan 10, 2011 6:09 pm
Reply with quote

Its good to use the above suggestion if u r doing in some QMF to just verify.
When you do it embedded, always use a cursor who knows u may get more than one second best salaries.
Back to top
View user's profile Send private message
Ronald Burr

Active User


Joined: 22 Oct 2009
Posts: 293
Location: U.S.A.

PostPosted: Mon Jan 10, 2011 7:28 pm
Reply with quote

manikawnth wrote:
When you do it embedded, always use a cursor who knows u may get more than one second best salaries.

If you are only retrieving column data that is qualified by MIN/MAX or other aggregate functions, there is no need to use a cursor.
There can logically be no instance where there could be more than one second highest salary. Even if there were a thousand qualifying rows that contain the same exact second highest salary, the MAX aggregate will result in only one returned value.
Back to top
View user's profile Send private message
manikawnth

New User


Joined: 07 Feb 2007
Posts: 61
Location: Mumbai

PostPosted: Mon Jan 10, 2011 8:17 pm
Reply with quote

Quote:
Even if there were a thousand qualifying rows that contain the same exact second highest salary, the MAX aggregate will result in only one returned value.

My intention was to say him, if he needs the employee details (like name) who get the 2nd highest salary, he needs to fetch it.
If he just needs the figure it is not necessary.
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