Premium problem76. Monthly Active User Retention

Hard Locked

activity(user_id, activity_date)

For each month of 2026, return how many users were active, how many of those were also active in the immediately previous calendar month, and the retention percentage.

All twelve months appear, including ones with no activity, where the counts are 0 and the percentage is NULL.

January looks back at December 2025, so activity from the previous year does count.

Return month as 'YYYY-MM', active_users, retained_users and retention_pct rounded to two decimals, ordered by month.

Tables

activity

user_id  activity_date
-------  -------------
1        2025-12-20
1        2026-01-05
1        2026-01-06
2        2026-01-10
1        2026-02-02
2        2026-04-01
1        2026-04-02

Expected result

month    active_users  retained_users  retention_pct
-------  ------------  --------------  -------------
2026-01  2             1               50.00
2026-02  1             1               100.00
2026-03  0             0               NULL
2026-04  2             0               0.00
2026-05  0             0               NULL
2026-06  0             0               NULL
2026-07  0             0               NULL
2026-08  0             0               NULL
... 4 more row(s)

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query