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 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 Loading data to table gives wrong for... Raghu navaikulam DB2 19 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