Premium problem51. Month-1 and Month-2 Cohort Retention

Medium Locked

users(user_id, signup_at) and activity(user_id, activity_date)

For each signup month, return the percentage of that cohort active in the first full calendar month after signup and the percentage active in the second.

Return signup_month as 'YYYY-MM', m1_retention_pct and m2_retention_pct rounded to two decimals, ordered by month.

Calendar months, not 30-day windows: somebody who signs up on 31 January and comes back on 2 February is retained in month 1, even though that is two days later.

Tables

activity

user_id  activity_date
-------  -------------
1        2026-02-02
2        2026-01-20
3        2026-02-11
3        2026-03-11
4        2026-04-01

users

user_id  signup_at
-------  ----------
1        2026-01-31
2        2026-01-02
3        2026-01-15
4        2026-02-10

Expected result

signup_month  m1_retention_pct  m2_retention_pct
------------  ----------------  ----------------
2026-01       66.67             33.33
2026-02       0.00              100.00

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