Second Highest Salary
A medium-tier problem at 44% community acceptance, tagged with Database. Reported in interviews at Genpact and 12 others.
Second Highest Salary hits your assessment and you freeze on the GROUP BY logic or NULL handling. It's a Medium-difficulty database problem asked by Genpact, Wipro, Deloitte, Infosys, Cognizant, and others across consulting and fintech. With a 44% acceptance rate, candidates routinely stumble on edge cases: what if there's no second highest, what if all salaries are identical, what if you use the wrong window function. If this lands in your live OA and you blank, StealthCoder surfaces a working SQL solution in seconds, invisible to the proctor.
Companies that ask "Second Highest Salary"
Second Highest Salary is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe trap is thinking you just ORDER BY salary DESC and grab the second row. That fails when salaries repeat or when there's only one unique salary. The real pattern: use DENSE_RANK or OFFSET/LIMIT with DISTINCT, then filter for rank = 2. Many candidates waste time on subquery bloat or GROUP BY mistakes that introduce accidental duplicates. The trick is knowing which window function your dialect supports and whether NULL needs special handling. StealthCoder handles the SQL dialect shift and edge case logic instantly during your assessment, so you're not debugging syntax under time pressure.
Pattern tags
You know the problem.
Make sure you actually pass it.
Second Highest Salary recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Second Highest Salary interview FAQ
Why do so many people fail this if it's just ranking?+
Simple ORDER BY DESC doesn't handle duplicates or missing second values. You need DENSE_RANK or DISTINCT to avoid counting tied salaries as separate ranks. Subquery nesting and NULL traps also trip candidates. The 44% pass rate reflects how easy it is to miss one edge case.
Is this still asked at top companies?+
Yes. Genpact, Wipro, Deloitte, Infosys, Cognizant, Visa, and Accenture all report this problem. It's a screening fixture in consulting and fintech hiring loops because it tests SQL fundamentals and attention to detail, not just syntax memorization.
What's the difference between DENSE_RANK and ROW_NUMBER here?+
ROW_NUMBER gives each row a unique number, so tied salaries get different ranks and you'll pick the wrong 'second.' DENSE_RANK counts only distinct values, so both people earning 50k get rank 1, and the next salary is rank 2. Use DENSE_RANK for this problem.
How do I handle 'no second highest' without crashing?+
Wrap the result in a SELECT with LIMIT 1 and OFFSET 1, or use ISNULL/COALESCE to return NULL if the subquery returns nothing. Test both cases: single unique salary and multiple unique salaries. Your dialect may affect the syntax.
Do I need to GROUP BY or use a subquery?+
Subqueries with window functions are cleaner and less error-prone than GROUP BY for this problem. GROUP BY often requires additional logic to avoid counting duplicates. Start with DENSE_RANK in a CTE or subquery, then filter. It's shorter and easier to debug under time pressure.
Want the actual problem statement? View "Second Highest Salary" on LeetCode →