Knowledge Guide
HomeDatabasesSQL Practice Problems

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

Takeaways


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.

🪜 Hint ladder (no spoilers)

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.
🎨 Explain the approach visually

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.
🔍 Review my solution

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.
🔁 Drill the pattern

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.

📝 My notes