See exactly why most candidates fail the second-highest salary SQL question. Compare a weak answer with a FAANG-level response that gets offers.
Write an SQL query to find the second-highest salary from an employee table.
You can use a subquery to find the second highest salary:
SELECT MAX(salary) FROM employee WHERE salary < (SELECT MAX(salary) FROM employee);
This finds the maximum salary that is less than the overall maximum, giving us the second highest.
There are three standard approaches, each with different trade-offs:
sqlSELECT MAX(salary) AS second_highest FROM employee WHERE salary < (SELECT MAX(salary) FROM employee);
Simple, but scans the table twice and fails silently when duplicates exist.
sqlSELECT salary AS second_highest FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employee ) ranked WHERE rnk = 2;
Handles duplicates correctly — DENSE_RANK assigns the same rank to tied salaries without gaps. Single pass.
sqlSELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT 1 OFFSET 1;
Concise for MySQL/PostgreSQL. Not ANSI-standard.
The difference between a junior and senior answer: juniors give ONE working query. Seniors demonstrate multiple approaches, explain trade-offs (duplicates, NULLs, performance), and show they can generalize the pattern.
Paste your answer and get instant AI-powered feedback with a FAANG-level improved version.
Analyze My Answer — Free3 free analyses per day. No sign-up required.