Rolling 30-Day Active Customers

SQL coding challenge · Difficulty: hard · Topic: Aggregation · +150 XP

Problem

For each date on which anything happened, count how many DISTINCT customers were active in the trailing 30 days — that date and the 29 before it.

A customer active twice in the window counts once. On 2024-01-15 the window reaches back to 2023-12-17, catching customers 1 and 2 from 2024-01-01 and customer 1 again on the day itself, so the answer is 2, not 3.

One row per distinct date, not one per activity row.

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 | active_customers_30d |
| --- | --- |
| 2023-06-01 | 1 |
| 2024-01-01 | 2 |
| 2024-01-15 | 2 |
| 2024-01-20 | 3 |
| 2024-02-05 | 3 |
| 2024-02-10 | 4 |

Explanation

The obvious COUNT(DISTINCT ...) OVER (...) does not work — MySQL does not allow DISTINCT inside a window function. Use a correlated subquery: for each date, count the distinct customers inside its own 30-day window.

Notes

What this SQL challenge teaches you

“Rolling 30-Day Active Customers” is a hard-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with COUNT DISTINCT, Correlated Subquery, Rolling Window, 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. COUNT(DISTINCT ...) is not allowed as a window function — a correlated subquery is the way.
  2. Drive the outer query off (SELECT DISTINCT activity_date FROM ...) so you get one row per date.
  3. BETWEEN d.activity_date - INTERVAL 29 DAY AND d.activity_date is a 30-day window that includes the current day.

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 hard and covers Aggregation.

Solve this challenge free on PySpark.in