Premium problem63. Friend Acceptance Rate by Month

Medium Locked

friend_requests(sender_id, receiver_id, requested_at) and friend_accepts(sender_id, receiver_id, accepted_at)

For each request month in 2026, return how many distinct sender-receiver pairs requested, how many were eventually accepted, and the acceptance rate.

A pair counts once per month however many times they requested. A pair is accepted if an acceptance exists at or after their first request that month.

Return request_month as 'YYYY-MM', request_pairs, accepted_pairs and acceptance_rate_pct rounded to two decimals, ordered by month.

Counting rows instead of pairs inflates both sides of the ratio, and unevenly, so the deduplication has to happen before the join.

Tables

friend_accepts

sender_id  receiver_id  accepted_at
---------  -----------  -------------------
1          2            2026-01-08 10:00:00
3          4            2026-01-01 10:00:00

friend_requests

sender_id  receiver_id  requested_at
---------  -----------  -------------------
1          2            2026-01-05 10:00:00
1          2            2026-01-06 10:00:00
1          2            2026-01-07 10:00:00
3          4            2026-01-10 10:00:00
5          6            2026-01-11 10:00:00
1          2            2026-02-01 10:00:00

Expected result

request_month  request_pairs  accepted_pairs  acceptance_rate_pct
-------------  -------------  --------------  -------------------
2026-01        3              1               33.33
2026-02        1              0               0.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