Write a SQL query to find the second highest salary in a table.
Model answer
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). Alternative with DENSE_RANK: SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees) t WHERE rnk = 2. The window function approach handles ties correctly — if two employees share the highest salary, DENSE_RANK still returns a meaningful second.