Get nth Highest and Lowest Salary of an Employee
Estimated study time: 3 minutes. A classic SQL interview question, solved three ways.
Finding the nth highest or lowest salary is one of the most common SQL interview questions, because it tests whether you understand subqueries, ranking functions, and pagination-style logic all at once.
Method 1: Using a Subquery
SELECT MIN(salary) FROM ( SELECT TOP 3 salary FROM employees ORDER BY salary DESC ) AS top_salaries;
This finds the 3rd highest salary by taking the top 3 salaries, then picking the smallest of that group.
Method 2: Using DENSE_RANK()
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees ) ranked WHERE rnk = 3;
DENSE_RANK() is the safest choice when duplicate salaries exist, since it won't skip ranks the way ROW_NUMBER() can behave unexpectedly with ties.
Method 3: Using OFFSET-FETCH
SELECT salary FROM employees ORDER BY salary DESC OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY;
Finding the Nth Lowest Salary
Simply reverse the sort order to ASC in any of the three methods above.
DENSE_RANK() when you want tied salaries to count as one rank; use ROW_NUMBER() when every row should be ranked individually regardless of ties.Why Interviewers Ask This
It reveals whether a candidate understands ordering, ranking, and pagination logic — skills used constantly in real-world reporting queries.