something urgent RRS feed

  • Question

  • i have 3 fields in a table

    empid      empname     empsalary

    i want get 2nd highest and 3rd highest salary and person name

    Wednesday, March 31, 2010 3:35 PM


  • You could select all of the data and then find the 2nd and 3rd in your code, but I assume you are looking for an SQL statement to do this at the database.

    If you are using SQL Server, here is a way to do it that can easily be adapted for other ranges.  Other database servers may or may not support this syntax or a similar syntax.  (This does not robustly address cases where two employees have the same salary.  It will need to be modified if that is important to you.)

    SELECT empname, empsalary
    SELECT ROW_NUMBER() OVER (ORDER BY empsalary DESC) rownum, empname, empsalary
    FROM employee
    ) inr
    WHERE inr.rownum BETWEEN 2 AND 3
    ORDER BY empsalary DESC

    To get an answer better targeted to your scenario, please provide more information.


    Thursday, April 1, 2010 10:42 PM