Skip to interview prep
Apexed

2. Write a query to find the second-highest salary.

MediumSQL

Technical
Show model answer

A portable approach: `SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);`

With window functions: `SELECT DISTINCT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees) t WHERE rnk = 2;` DENSE_RANK handles ties so duplicates of the top salary do not count as second.

Write a query to find the second-highest salary. — Data & analytics interview question — Apexed