Premium problem40. Purchase Frequency Histogram

Medium Locked

purchases(purchase_id, user_id, purchased_at)

For September 2026, return a histogram: for each purchase count, how many users made exactly that many purchases.

Return purchase_count and user_count, ordered by purchase_count.

Users with no September purchases do not appear, since they have no count to be bucketed under.

Tables

purchases

purchase_id  user_id  purchased_at
-----------  -------  ------------
1            10       2026-09-01
2            11       2026-09-02
3            12       2026-09-03
4            12       2026-09-04
5            13       2026-09-05
6            13       2026-09-06
7            14       2026-09-07
8            14       2026-09-08
... 3 more row(s)

Expected result

purchase_count  user_count
--------------  ----------
1               2
2               2
3               1

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