Third Highest Salary

  • I have the table named employee with the two columns ie emp_id and salary. I want to fetch the third highest salary from the list of records.

    I have seen many sites but they all talk about second highest. plz help me out.

  • create table #employee (emp_id int, salary int)

    insert into #employee values(1, 24000)

    insert into #employee values(2, 34000)

    insert into #employee values(3, 54000)

    insert into #employee values(6, 44000)

    insert into #employee values(4, 44000)

    insert into #employee values(5, 34000)

    select

     nth_max_salary = max([salary])

    from [#employee]

    where [salary]

     not in

     ( select top 2

      [salary]

     from [#employee]

     order by [salary] desc)

    drop table [#employee]

    Regards,
    Leon Bakkers

  • Another way would be the following

    select min(salary) as Salary from (select top 3 salary from employees order by salary desc) tSalaries

    If the phone doesn't ring...It's me.

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

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