Sql Query to Retrieve Nth Highest Salary From Table in SQL Server

SQL Server Faqs

SQL Query to find Nth Highest Salary in SQL Server.

SELECT * FROM tablename e1 WHERE N = (SELECT COUNT(DISTINCT (e2.sal))
FROM tablename e2 WHERE e2.sal >= e1.sal)

For example to get 3rd highest salary:
SELECT * FROM emp e1 WHERE 3 = (SELECT COUNT(DISTINCT (e2.sal))FROM emp e2 WHERE e2.sal >= e1.sal)

Similarly to get 2nd highest salary:
SELECT * FROM emp e1 WHERE 2 = (SELECT COUNT(DISTINCT (e2.sal))FROM emp e2 WHERE e2.sal >= e1.sal)

SQL Server Faqs
SQL Query to copy only structure of one table to other table in SqlServer
SQL Query to get structure of table in SqlServer

Movie Songs Lyrics
Software Testing-QTP Scripts,Testing Faqs

1 comment:

Mayur Prakash said...

Thanx buddy !!!
It helped me a lot..
Nice work.

Recent Posts