File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes second highest value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "second highest value" Watch "second highest value" New topic
Author

second highest value

deepak carter
Ranch Hand

Joined: Feb 19, 2011
Posts: 165
how to find the second highest salry

i am using this logic....

select * from emp e where
2 =(select count(distinct sal) from emp where e.sal<=sal)

but need an alternative


Thanks in advance
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Which database system are you using?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 556
    
    7

Find highest.... Delete that record (or make salary negative)...find highest. <- there you are
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

I would assume that a non-desctructive means is required.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

P.S. Do you really think that sal is a better name than salary?

Why people insist on freeze-drying perfectly good words is beyond my capacity to imagine.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

Bear Bibeault wrote:P.S. Do you really think that sal is a better name than salary?

Why people insist on freeze-drying perfectly good words is beyond my capacity to imagine.

Deepak is probably not guilty in this case: emp and sal are parts of the (in)famous scott/tiger Oracle sample schema.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

Deepak, if you're indeed on Oracle, you might use analytic functions to do this:

Other databases may also support analytics.

However, "second highest salary" is a bit ambiguously defined. Consider salaries 5000, 5000, 4000. Which of them is second highest? One interpretation (the one I used in the above query) is that 4000 is the second highest. Another interpretation is that there is no second-highest salary, there are two maximum salaries and one third-highest salary. Depending on context, one or the other of these interpretations can be the "right" one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: second highest value
 
Similar Threads
SQL query help
find second largest value in a column
Get Maximum sal from emp table
limiting the number of rows returned from the database
How to get top 3 earners in emp table of SCOTT's schema using SQL