Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
How to get 2nd highest row from a table?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: How to get 2nd highest row from a table?
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    Post subject:
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    Post subject: Reply to: How to get 2nd highest row from a table?
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    Post subject:
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    Post subject:
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    Post subject:
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    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 Update table without cursor Ron Klop DB2 3 Wed Oct 11, 2017 1:06 pm
No new posts Table(Unicode(Graphic) table) loading... muralikrishnan_new DB2 0 Thu Oct 05, 2017 5:10 pm
No new posts Insert a Row_number into table useit DB2 2 Tue Sep 19, 2017 1:07 pm
No new posts how to see when the last read access ... Mike 1304 DB2 1 Tue Sep 12, 2017 7:52 pm
No new posts PC (UTF-8) -> z/OS (EBCDIC) -> ... prino All Other Mainframe Topics 4 Fri Sep 01, 2017 1:47 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us