Cumulative Active Users Over Time

SQL coding challenge · Difficulty: medium · Topic: Aggregation · +100 XP

Problem

For each date, count how many DISTINCT customers have ever been active up to and including that date. This is the 'total users so far' line on a growth chart, so it never goes down.

A customer active on several dates is counted once, on their first appearance — so the number only rises when someone NEW shows up. Between 2024-01-01 and 2024-01-15 it stays flat, because 2024-01-15 was customer 1 returning.

One row per distinct date.

Schema — `user_activity`

| Column | Type |
| --- | --- |
| customer_id | INT |
| activity_date | DATE |

Example Input — `user_activity`

| customer_id | activity_date |
| --- | --- |
| 5 | 2023-06-01 |
| 1 | 2024-01-01 |
| 2 | 2024-01-01 |
| 1 | 2024-01-15 |
| 3 | 2024-01-20 |
| 2 | 2024-02-05 |
| 4 | 2024-02-10 |

Expected Output

| activity_date | cumulative_users |
| --- | --- |
| 2023-06-01 | 1 |
| 2024-01-01 | 3 |
| 2024-01-15 | 3 |
| 2024-01-20 | 4 |
| 2024-02-05 | 4 |
| 2024-02-10 | 5 |

Explanation

It differs from a rolling window in that nothing ever leaves: customer 5 was active once in June 2023 and still counts in February 2024.

Notes

What this SQL challenge teaches you

“Cumulative Active Users Over Time” is a medium-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with COUNT DISTINCT, Correlated Subquery, Cumulative, Aggregation — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Same shape as a rolling count, but with no lower bound on the window.
  2. COUNT(DISTINCT ...) cannot be a window function, so use a correlated subquery with WHERE a.activity_date <= d.activity_date.
  3. The number must never decrease — if it does, you are counting rows in a period rather than distinct customers so far.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Aggregation.

Solve this challenge free on PySpark.in