nth highest salary

  •  select *

      from bills a WHERE 3= ( select count(*)

                    from bills b  WHERE b.amount > a.amount )

     

    When I was searching for the nth highest salary I found the above query. But I didn't understan how it works and what happens when we say where 3=

    Thanks.

  • it is simply an equality operation A = 3 is the same as 3 = A and you are simply searching for those who have 3 counted records with higher values than the current one.

    Cheers,

     


    * Noel

  • sql_2005_fan:

    It means you are looking for an amount from "bills a" for which there are exactly 3 amounts which are higher in "bills b".

    This means that you are looking for the 4th highest amount in "bills".  The "3" would be "nth - 1".

     

  • And if you really are a sql_2005_fan, you should not be using this and should be using the RANK/OVER functions in  SQL 2005 as a more efficient alternative.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply