Second Highest Salary
Reported by candidates from Hartford Financial Services's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Hartford Financial Services reported this one in October 2026, and it's a SQL question that sounds harder than it is. Strip the wording and it's one thing: rank distinct salaries, grab the second, and return NULL when it doesn't exist. That last clause is where people lose points. You need exactly one row, one column named second_highest_salary, even on an empty table. If you blank on the NULL-row requirement, StealthCoder runs invisibly during the live OA as a safety net and hands you the shape of the query.
The problem
The table employees contains salary values. Find the second-highest distinct, non-NULL salary. Equal salaries share one rank. Ignore rows whose salary is NULL. Return exactly one row and one column named second_highest_salary. If fewer than two distinct non-NULL salaries exist, that row must contain NULL. Tables employees: salary (Integer) Constraints employees contains between 0 and 2000 rows. Each known salary is an integer from 0 through 10^9, inclusive. The table may contain duplicate salary values and NULL salaries.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is distinctness plus the guaranteed row. Two clean routes. First: SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). Aggregates with no matching rows return NULL, so the empty and single-value cases handle themselves. NULLs are ignored by MAX, which covers the NULL rule. Second: wrap SELECT DISTINCT salary... ORDER BY salary DESC LIMIT 1 OFFSET 1 inside a subquery, so the outer SELECT yields NULL when the inner returns nothing. The pitfall is a bare LIMIT 1 OFFSET 1, which returns zero rows instead of a NULL row. Another is DENSE_RANK without DISTINCT thinking, or forgetting the alias. Ties share one rank, so plain ROW_NUMBER is wrong. If the live OA freezes you, StealthCoder is the hedge that reads the prompt and gives you the working query.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Second Highest Salary cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as second highest salary. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Hartford Financial Services's OA.
Hartford Financial Services reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Second Highest Salary FAQ
What's the trick in Second Highest Salary?+
Return a guaranteed single row. Use MAX(salary) with a WHERE salary < (SELECT MAX(salary)...) filter. Aggregates return NULL on empty input, so you never have to special-case tables with fewer than two distinct salaries.
Why does LIMIT 1 OFFSET 1 fail here?+
When there's no second distinct salary, it returns zero rows, not a row containing NULL. The problem demands exactly one row. Wrap it as a scalar subquery in an outer SELECT, and the missing result becomes NULL automatically.
Do duplicates and NULL salaries break the MAX approach?+
No. Duplicates of the top value are all excluded by the less-than filter, so the next value is the true second distinct one. MAX skips NULLs on its own, and the comparison drops NULL rows from the WHERE clause.
Can I use DENSE_RANK instead?+
Yes. Rank salaries descending with DENSE_RANK, filter rank = 2, and take MAX of that salary so you still get a NULL row when nothing matches. Don't use ROW_NUMBER or RANK, since they mishandle ties and gaps.
How do I prepare for this in 48 hours?+
Write the MAX subquery version and the OFFSET version from memory, then test them on an empty table, one row, all-equal salaries, and NULL-only salaries. Those four edge cases are exactly what the Hartford Financial Services prompt spells out.