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

Write an SQL query to find the second highest distinct salary from the employee table.

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

💡 Model Answer

The approach is identical to the previous question. Use a subquery with ORDER BY and LIMIT/OFFSET:

SELECT DISTINCT salary

FROM Employee

ORDER BY salary DESC

LIMIT 1 OFFSET 1;

Or use a window function:

SELECT salary

FROM (

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

FROM Employee

) AS ranked

WHERE rnk = 2;

Both queries return the second highest distinct salary. They operate in O(n log n) time due to sorting and handle duplicate salaries by eliminating them 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