Average Workout Length Before and After Joining
Problem
Table: `gym_visits`
```text
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| member_id | int |
| visit_date | date |
| pass_kind | varchar |
| minutes | int |
+--------------+---------+
There is no single-column primary key; a member may have many visits.
pass_kind is one of 'trial', 'member', or 'lapsed'.
minutes is how long that visit lasted.
```
A gym wants to study members who started on a `trial` pass and later upgraded to a full `member` pass. A member counts as converted only if they have at least one `trial` visit and at least one `member` visit.
For each converted member, return their average visit length during the trial phase and during the member phase. Round both averages to two decimals and ignore `lapsed` visits entirely. Return `member_id`, `trial_avg_minutes`, and `member_avg_minutes`, ordered by `member_id`.
Tables
Example rows — the live problem includes the full dataset.
| member_id | visit_date | pass_kind | minutes |
|---|---|---|---|
| 1 | 2024-01-01 | trial | 45 |
| 1 | 2024-01-02 | trial | 30 |
| 1 | 2024-01-05 | trial | 60 |
Expected output
Your answer should return 3 rows with the columns member_id, trial_avg_minutes, member_avg_minutes.
Starter code (SQL)
SELECT *
FROM gym_visits;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