aspose 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
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: 19794
    
  20

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: 582
    
    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: 61774
    
  67

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: 61774
    
  67

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: 3611
    
  60

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: 3611
    
  60

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