Average Workout Duration per Treadmill
Problem
Table: `treadmill_log`
| Column Name | Type |
|---|---|
| treadmill_id | int |
| session_id | int |
| phase | text |
| moment | float |
`(treadmill_id, session_id, phase)` is the primary key. Each treadmill in a gym records two rows per workout session: one row with `phase = 'start'` and one with `phase = 'finish'`, where `moment` is the timestamp in seconds.
For every treadmill, compute the **average workout duration** across all of its sessions. The duration of a session is its finish timestamp minus its start timestamp. Round the answer to 3 decimal places and alias it `avg_duration`. Return one row per treadmill.
Tables
Example rows — the live problem includes the full dataset.
| treadmill_id | session_id | phase | moment |
|---|---|---|---|
| 1 | 10 | start | 100 |
| 1 | 10 | finish | 130 |
| 1 | 11 | start | 200 |
Expected output
Your answer should return 2 rows with the columns treadmill_id, avg_duration.
Starter code (SQL)
SELECT *
FROM treadmill_log;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock