Fast Submissions, Long Totals
Given participants(id, name, group_name) and submissions(submission_id, participant_id, start_time, end_time, verdict), return the participants whose total time across all submissions exceeds 30 minutes while every one of their submissions finished within 10 minutes. Order by total time ascending.
Then be ready for the follow-ups: rank participants by total time within each group, find each participant's longest submission, and compute a running total of time per participant ordered by start time.
Hints
Hint 1
Filtering rows with WHERE duration <= 10 minutes before aggregating keeps a participant who had an 11-minute submission; the all-submissions condition is HAVING MAX(duration) <= 10 minutes.
Hint 2
Compute the duration once in a CTE in the unit you will compare against (seconds or an interval), and use that unit consistently.
Solution
Show the approach
Approach
WITH timed AS (
SELECT participant_id,
EXTRACT(EPOCH FROM (end_time - start_time)) AS seconds
FROM submissions
)
SELECT p.id, p.name, SUM(t.seconds) / 60.0 AS total_minutes
FROM participants p
JOIN timed t ON t.participant_id = p.id
GROUP BY p.id, p.name
HAVING SUM(t.seconds) > 30 * 60
AND MAX(t.seconds) <= 10 * 60
ORDER BY total_minutes ASC;Both conditions are properties of the group, so both live in HAVING. The row-level version, WHERE seconds <= 600, is the trap: a participant with one 12-minute and four 8-minute submissions passes it with a 32-minute total, yet should be excluded.
The follow-ups
-- rank by total time within each group
SELECT group_name, name, total_minutes,
RANK() OVER (PARTITION BY group_name ORDER BY total_minutes DESC) AS rank_in_group
FROM totals;
-- each participant's longest submission
SELECT * FROM (
SELECT s.*, ROW_NUMBER() OVER (PARTITION BY participant_id ORDER BY end_time - start_time DESC) AS rn
FROM submissions s
) x WHERE rn = 1;
-- running total per participant, in start order
SELECT participant_id, start_time,
SUM(EXTRACT(EPOCH FROM (end_time - start_time)))
OVER (PARTITION BY participant_id ORDER BY start_time
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_seconds
FROM submissions;Worth saying out loud
ROW_NUMBERpicks exactly one row per partition;RANKleaves gaps after ties andDENSE_RANKdoes not. Say which tie behaviour the question wants.- The frame clause matters for running totals: the default frame with
ORDER BYisRANGE, which lumps equal start times together;ROWSgives the per-row running sum. - The schema as given has no participant column on
submissions; call that out and state the key you assume before writing anything. - Participants with zero submissions never appear; a
LEFT JOINwithCOALESCE(SUM, 0)includes them if asked.
Assistant
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.