Premium problem53. Subscribers Active at Month End

Medium Locked

subscriptions(subscription_id, user_id, started_at, ended_at)

For each of the twelve month-end dates of 2026, return how many distinct users had at least one subscription active on that date.

ended_at is inclusive, so a subscription ending on 31 March is still active that day. NULL means it never ended.

Return month_end and active_users, ordered by date. Months where nobody was subscribed appear with 0.

A user with two overlapping subscriptions is still one user.

Tables

subscriptions

subscription_id  user_id  started_at  ended_at
---------------  -------  ----------  ----------
1                10       2026-01-15  NULL
2                10       2026-02-01  2026-03-31
3                11       2026-02-10  2026-02-20
4                12       2026-06-01  NULL

Expected result

month_end   active_users
----------  ------------
2026-01-31  1
2026-02-28  1
2026-03-31  1
2026-04-30  1
2026-05-31  1
2026-06-30  2
2026-07-31  2
2026-08-31  2
... 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