HomeInterview QuestionsWrite a simple SQL query to fetch the third highes…

Write a simple SQL query to fetch the third highest salary from the employee table.

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

💡 Model Answer

One concise way to get the third highest distinct salary is to use a window function:

SELECT salary

FROM (

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

FROM employee

) AS ranked

WHERE rnk = 3;

Explanation:

  • The inner query assigns a dense rank to each distinct salary in descending order.
  • The outer query filters for rank 3, which corresponds to the third highest salary.

If your database does not support window functions, you can use a subquery with LIMIT/OFFSET (MySQL) or OFFSET (PostgreSQL):

SELECT DISTINCT salary

FROM employee

ORDER BY salary DESC

LIMIT 1 OFFSET 2;

Both approaches return the third highest unique salary. Complexity is O(n log n) due to sorting, but the database engine optimizes the query internally.

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