Schema
Table
Setup
| Column Name | Type | Description |
|---|---|---|
| user_id | integer | User identifier. |
| watch_date | date | Date watched. |
Find the number of active users per day.
Schema
| Column Name | Type | Description |
|---|---|---|
| user_id | integer | User identifier. |
| watch_date | date | Date watched. |
Sample Data
| user_id | watch_date |
|---|---|
| 101 | 2024-01-01 |
| 102 | 2024-01-01 |
| 101 | 2024-01-02 |
| 103 | 2024-01-02 |
| 104 | 2024-01-03 |
| 105 | 2024-01-03 |
| 106 | 2024-01-03 |
| watch_date | active_users |
|---|---|
| 2024-01-01 | 2 |
| 2024-01-02 | 2 |
| 2024-01-03 | 3 |
SQL Editor
| watch_date | active_users |
|---|---|
| 2024-01-01 | 2 |
| 2024-01-02 | 2 |
| 2024-01-03 | 3 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT watch_date, COUNT(DISTINCT user_id) AS active_users FROM watching_activity GROUP BY watch_date ORDER BY watch_date;
Explanation
Group rows by watch_date.
Count unique users in each date group.
Return one row per day.
Related Questions