medium Second Fastest Time in Races
Second-smallest time — and the edge case the old query got wrong
RaceResults(id, time). Return the second-fastest (second-smallest) time, or
NULL when there's no second one (e.g. a single participant). The earlier solution —
SELECT MAX(time) FROM (SELECT time FROM RaceResults GROUP BY time ORDER BY time ASC LIMIT 2) — gives the
right answer for many inputs, but its claim that "one participant → NULL" is false: with one distinct
time the inner query returns a single row and MAX returns that time, not NULL. Use a
form that genuinely yields NULL when no second value exists:
-- Approach 1: distinct, ordered, skip one; wrap so the empty case is NULL
SELECT (SELECT DISTINCT time
FROM RaceResults
ORDER BY time ASC
LIMIT 1 OFFSET 1) AS SecondFastestTime;
-- Approach 2: smallest time strictly greater than the minimum
SELECT MIN(time) AS SecondFastestTime
FROM RaceResults
WHERE time > (SELECT MIN(time) FROM RaceResults);
Traced
- Times
{60, 55, 58, 59}: distinct sorted55, 58, 59, 60→OFFSET 1→ 58. Approach 2:MIN(time > 55)= 58. ✓ - One participant
{55}:OFFSET 1returns no row → scalar subquery → NULL. Approach 2:WHERE time > 55is empty →MIN→ NULL. ✓ (the old MAX-of-top-2 would have returned 55.)
Takeaways
- "Second smallest" with a NULL-when-absent guarantee:
DISTINCT … LIMIT 1 OFFSET 1wrapped as a scalar, orMIN(x) WHERE x > MIN(x). - Always test the single-row / all-equal edge case — that's where
MAX-of-top-N silently breaks.
Re-authored for correctness for this guide (the prior MAX-of-top-2 returns the only time instead of NULL for one participant). See also: 2nd Highest Salary, LIMIT and OFFSET, Aggregate Functions.
🤖 Don't fully get this? Learn it with Claude
Stuck on Second Fastest Time in Races? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Second Fastest Time in Races** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Second Fastest Time in Races** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Second Fastest Time in Races**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Second Fastest Time in Races**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.