Friend Requests Acceptance Rate
Problem
Schemas: FriendRequest(sender_id INT, receiver_id INT, request_date DATE) and RequestAccepted(requester_id INT, accepter_id INT, accept_date DATE).
Return the overall acceptance rate: distinct accepted pairs ÷ distinct requested pairs, rounded to 2 decimals. If there are no requests, return 0.
Constraints
- (sender, receiver) may repeat; count distinct.
Approach — Core Patterns
This is a Core Patterns problem. The idea: pick the data structure best matched to the constraints and solve it in one clean pass. Work through the reference code below line by line, then re-derive it yourself in the editor — that's how the pattern sticks.
Complexity: see the walkthrough below.
Solution code
SQL
SELECT ROUND(
COALESCE(
(SELECT COUNT(*) FROM (SELECT DISTINCT requester_id, accepter_id FROM RequestAccepted)) * 1.0
/ NULLIF((SELECT COUNT(*) FROM (SELECT DISTINCT sender_id, receiver_id FROM FriendRequest)), 0),
0
), 2
) AS acceptance_rate;
Practice it
Reading a solution isn't the same as being able to write it under pressure. Open this problem in the in-browser editor, hide the solution, and solve it from scratch — your code runs against real test cases instantly.
Solve Friend Requests Acceptance Rate interactively → ← All solutions