Daily Distinct Gym Visitors
Problem
Table: `CheckIns`
```text
+-------------+---------+
| Column | Type |
+-------------+---------+
| member_id | int |
| visit_id | int |
| visit_day | date |
| zone | varchar |
+-------------+---------+
There is no single-column primary key (a member can check in many times a
day). Each row is one entry through the turnstile.
```
A member is *active* on a day if they have at least one check-in that day.
For each day in the 30-day window ending **2024-05-31 inclusive** (that is,
from 2024-05-02 to 2024-05-31), return the day and the number of distinct
active members. Use the column headings `day` and `active_members`. Only
include days that actually have at least one check-in.
**Example**
```text
CheckIns:
+-----------+----------+------------+--------+
| member_id | visit_id | visit_day | zone |
+-----------+----------+------------+--------+
| 1 | 10 | 2024-05-02 | weights|
| 1 | 11 | 2024-05-02 | cardio |
| 2 | 12 | 2024-05-02 | pool |
| 3 | 13 | 2024-05-31 | weights|
+-----------+----------+------------+--------+
Output:
+------------+----------------+
| day | active_members |
+------------+----------------+
| 2024-05-02 | 2 |
| 2024-05-31 | 1 |
+------------+----------------+
```
On 2024-05-02 members 1 and 2 were active (member 1's two check-ins count
once). On 2024-05-31 only member 3 was active.
Tables
Example rows — the live problem includes the full dataset.
| member_id | visit_id | visit_day | zone |
|---|---|---|---|
| 1 | 10 | 2024-05-02 | weights |
| 1 | 11 | 2024-05-02 | cardio |
| 2 | 12 | 2024-05-02 | pool |
Expected output
Your answer should return 2 rows with the columns day, active_members.
Starter code (SQL)
SELECT *
FROM CheckIns;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