Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Please explain this query

Re: Please explain this query

From: <jkstill_at_gmail.com>
Date: 13 Oct 2005 15:50:20 -0700
Message-ID: <1129243820.775389.65400@o13g2000cwo.googlegroups.com>

shihab wrote:
> This is the query to findout Nth Max (Highest) value from a table
>
> SELECT sal from emp t
> WHERE &n = (SELECT COUNT(sal)
> FROM (SELECT DISTINCT sal FROM emp)
> WHERE t.sal<=sal);
>
> Can anybody expalin how this query works.....
>
>
> Thanks in advance
> Shihab

That is a bit of a puzzler, isn't it?

Try re-arranging the SQL a bit, and it may become more clear.

SELECT sal,
(

   SELECT COUNT(sal)
   FROM (SELECT DISTINCT sal FROM emp)    WHERE t.sal<=sal
) rank
from emp t
order by sal Received on Thu Oct 13 2005 - 17:50:20 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US