D1, D7 and D30 Retention per User

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

Problem

A user is 'retained on day N' if they did anything exactly N days after they signed up. Report each user's D1, D7 and D30 retention as 1 or 0.

Also return retention_pattern: the three flags joined with -, so user 2 reads 1-0-0. It is the same information in one field, and it makes a wrong pattern obvious at a glance.

Schema — `signups`

| Column | Type |
| --- | --- |
| user_id | INT |
| signup_date | DATE |

Schema — `user_events`

| Column | Type |
| --- | --- |
| user_id | INT |
| event_date | DATE |

Example Input — `signups`

| user_id | signup_date |
| --- | --- |
| 1 | 2024-01-01 |
| 2 | 2024-01-01 |
| 3 | 2024-01-05 |

Example Input — `user_events`

| user_id | event_date |
| --- | --- |
| 1 | 2024-01-02 |
| 1 | 2024-01-08 |
| 1 | 2024-01-31 |
| 2 | 2024-01-02 |
| 3 | 2024-01-12 |

Expected Output

| user_id | retained_d1 | retained_d7 | retained_d30 | retention_pattern |
| --- | --- | --- | --- | --- |
| 1 | 1 | 1 | 1 | 1-1-1 |
| 2 | 1 | 0 | 0 | 1-0-0 |
| 3 | 0 | 1 | 0 | 0-1-0 |

Explanation

Retention is measured against each user's OWN signup date, not a fixed calendar date — user 3 signed up on 2024-01-05, so their day 7 is 2024-01-12.

Worked through: user 1 returns on day 1, day 7 and day 30, so 1/1/1. User 2 returns on day 1 and never again, so 1/0/0. User 3 returns only on day 7, so 0/1/0.

Every user must appear, including those who never came back — which is why the join has to be a LEFT JOIN.

Notes

What this SQL challenge teaches you

“D1, D7 and D30 Retention per User” is a hard-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with LEFT JOIN, DATEDIFF, CASE WHEN, Retention — 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. DATEDIFF(event_date, signup_date) gives the day offset for each event.
  2. MAX(CASE WHEN offset = 7 THEN 1 ELSE 0 END) turns 'any event on day 7' into a 1/0 flag per user.
  3. Use a LEFT JOIN so a user with no events still appears, with zeros.
  4. CONCAT_WS('-', flag1, flag7, flag30) builds the pattern.

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