how to retrive the second highest value from the table

  • Hi all

    I want to knw how i can retrieve the second highest value from the table which has some data

    like the detail of employe and i want to retrieve the name of the employe who has second highest salary in the detail table

    pls its very urgent

    reply soon

    thanks in advance/

  • you should be able to use ROW_NUMBER() for this, have a look at BOL for the syntax, or post your table defs and some sample data for further help..

  • Try this:

    DECLARE @employees TABLE (

    empId int,

    name varchar(50)

    )

    DECLARE @salaries TABLE (

    empId int,

    salary money

    )

    INSERT INTO @employees VALUES (1, 'John')

    INSERT INTO @employees VALUES (2, 'Mary')

    INSERT INTO @employees VALUES (3, 'Jane')

    INSERT INTO @employees VALUES (4, 'Leo')

    INSERT INTO @salaries VALUES (1, 1000)

    INSERT INTO @salaries VALUES (2, 2000)

    INSERT INTO @salaries VALUES (3, 3000)

    INSERT INTO @salaries VALUES (4, 4000)

    SELECT emp.empId, emp.Name, Sqry.salary

    FROM @employees AS emp

    INNER JOIN (

    SELECT TOP 1 empId, salary

    FROM (

    SELECT TOP 2 empId, salary

    FROM @salaries

    ORDER BY salary DESC

    ) AS TopTwo

    ORDER BY salary ASC

    ) AS Sqry

    ON emp.empId = Sqry.empId

    -- Gianluca Sartori

  • WITH max_salaries (Salary) AS

    ( SELECT TOP 2 Salary FROM Workers ORDER BY Salary DESC ) -- returns the two higher salaries

    SELECT min(Salary) FROM max_salaries -- returns the lower salary from the two selected above: his is actually the second highest value

  • Check out the RANK function in BOL. Something like this might work (Uses adventureworks):

    ;WITH cteRank AS

    (

    SELECT

    EPH.EmployeeID,

    EPH.Rate,

    RANK() OVER(ORDER BY Rate desc) AS ranking

    FROM

    HumanResources.EmployeePayHistory AS EPH JOIN

    (

    SELECT

    EmployeeID,

    MAX(RateChangeDate) AS last_date

    FROM

    HumanResources.EmployeePayHistory AS EPH2

    GROUP BY

    EPH2.EmployeeID

    ) AS LastRate ON

    EPH.EmployeeID = LastRate.EmployeeID AND

    EPH.RateChangeDate = LastRate.last_date

    )

    SELECT

    *

    FROM

    cteRank

    WHERE

    ranking = 2

  • anu1krishna (5/12/2009)


    pls its very urgent

    Hey,

    Have you considered the possible benefits of having a go at these (presumably homework) questions?

    Sure posting on here and kicking back may seem cool, but how much are you actually learning...?

    Just a thought!

    Paul

  • i tried like this

    declare @id int

    set @id=select top 2 id_num from new_employes order by id_num desc

    select id_num from new_employes where id_num=(select min(@id)from new_employes)

    but i am getting error please say what is incorrect in it

  • anu1krishna (5/16/2009)


    i tried like this

    declare @id int

    set @id=select top 2 id_num from new_employes order by id_num desc

    select id_num from new_employes where id_num=(select min(@id)from new_employes)

    but i am getting error please say what is incorrect in it

    Everything seems to be wrong with this query.....

    1. set @id=select top 2 id_num from new_employes order by id_num desc

    You can only store a single value in a variable declared as int.

    2.select id_num from new_employes where id_num=(select min(@id)from new_employes)

    Since you cant store more than one value in the variablr @id, how can you apply min function on that variable.......

    3. and plenty more errors ......

    My suggestion is ...Get your basics right....!

  • thx for ur suggestion:w00t:

  • thx fro your quick reply it is really a good idea to use rank function

  • thx dear it really works fine 🙂

  • why not just grab the TOP 2? and choose the min of the 2?

  • Andrej Milas (5/18/2009)


    why not just grab the TOP 2? and choose the min of the 2?

    That is what Gianluca Sartori posted earlier on in the first page, I guess it is more efficent? to use CTEs and Rank(). Also as this seems like an exam question it is probably better to learn Rank() at some stage..

  • Hi,

    Try out this query.Using this u can find Nth highest salary.

    declare @n int

    set @n=2

    Select * From Employee E1 Where

    (@n-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where

    E2.Salary > E1.Salary)

    Thanks

    Shailesh

  • select max(salary) from employee_details e1

    where 2 <=(select count(*) from employee_details e2 where e1.employeeid <= e2.employeeid)

Viewing 15 posts - 1 through 15 (of 22 total)

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