Ever wondered how to retrieve the employee with the Nth highest salary from a database? This short explains the logic using SQL to find the second highest salary, a common interview question.
Key insight: Use a subquery with
DISTINCTandORDER BYto rank salaries, then applyLIMITandOFFSETto pinpoint the Nth position.
The technique works for any N by adjusting the offset. Master this pattern to handle ranking queries with ease.