Prime-Time Gym Regulars
Problem
A fitness club records every workout in the table `gym_visits`, one row per visit, with the `member_id`, the `checkin_time` the member started, the `calories_burned`, and an optional `session_score` from 1 to 5 that the member may leave afterwards (NULL when they skip the rating). Find the *prime-time regulars*, defined by ALL of the following: the member logged at least 3 visits; at least 60% of their visits started during prime time (hours 06, 07, 08 in the morning, or 17, 18, 19 in the evening); the average score over the visits they DID rate is at least 4.0; and they rated at least half of their visits. For each qualifying member return their `member_id` and their average rated score as `mean_score`, rounded to 2 decimals. Order by `mean_score` descending, then by `member_id` descending.
Tables
Example rows — the live problem includes the full dataset.
| visit_id | member_id | checkin_time | calories_burned | session_score |
|---|---|---|---|---|
| 1 | 10 | 2024-04-01 06:30:00 | 420 | 5 |
| 2 | 10 | 2024-04-03 18:10:00 | 510 | 4 |
| 3 | 10 | 2024-04-05 07:45:00 | 380 | 5 |
Expected output
Your answer should return 2 rows with the columns member_id, mean_score.
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