HomeInterview QuestionsWrite an SQL query to find the second highest dist…

Write an SQL query to find the second highest distinct salary in the Employee table.

🟡 Medium Coding Junior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

To get the second highest distinct salary, you can order the distinct salaries in descending order and skip the first row. One simple method is:

SELECT DISTINCT salary

FROM Employee

ORDER BY salary DESC

LIMIT 1 OFFSET 1;

This returns the second row after sorting, which is the second highest distinct salary. If your database supports window functions, you can use DENSE_RANK:

SELECT salary

FROM (

SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk

FROM Employee

) AS ranked

WHERE rnk = 2;

Both queries run in O(n log n) time due to the sort. They handle duplicate salaries correctly because DISTINCT or DENSE_RANK removes duplicates before ranking.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500