how to find nth highest salary

Answer Posted / himanshu

Just replace the N with required number:


SELECT empid, empname, empsalary FROM emp e
WHERE (SELECT COUNT(DISTINCT(e2.empsalary))
FROM emp e2
WHERE e2.empsalary >=
e.empsalary ) = N

ex: 4th highest

SELECT empid, empname, empsalary FROM emp e
WHERE (SELECT COUNT(DISTINCT(e2.empsalary))
FROM emp e2
WHERE e2.empsalary >=
e.empsalary ) = 4

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by sql server agent?

631


What is scrollable cursor?

647


Why we use trigger in sql server with example?

606


In case you have filters in your report, when filters will be applied in cached report instance?

83


Explain the xml support sql server extends?

707






can you implement data mining in SSRS?

117


Can we insert data if clustered index is disabled?

603


What is @@rowcount in sql?

637


can a database be shrunk with users active? : Sql server administration

649


Give the query of getting last two records from the table in SQL SERVER?

631


Do you know what is difference between index seek vs. Index scan?

597


How do you run a trace?

610


Can a unique index be created on a column, which contains null?

612


what are cursors? : Sql server database administration

616


What is the server name for sql management studio?

549