Showing posts with label nth highest salary. Show all posts
Showing posts with label nth highest salary. Show all posts

Thursday, July 16, 2015

Sql query to get nth highest salary

  • Query 1

  • SELECT DISTINCT(salary) FROM table_name tn1 WHERE (n-1) = (SELECT COUNT( DISTINCT( tn2.salary )) FROM table_name tn2 WHERE tn2.salary > tn1.salary
  • Query 2

  • SELECT TOP 1 salary FROM (SELECT DISTINCT TOP n FROM table_name ORDER BY saalary DESC) tn ORDER BY salary