Table: FriendRequest
+----------------+---------+ | Column Name | Type | +----------------+---------+ | sender_id | int | | send_to_id | int | | request_date | date | +----------------+---------+ This table may contain duplicates (In other words, there is no primary key for this table in SQL). This table contains the ID of the user who sent the request, the ID of the user who received the request, and the date of the request.
Table: RequestAccepted
+----------------+---------+ | Column Name | Type | +----------------+---------+ | requester_id | int | | accepter_id | int | | accept_date | date | +----------------+---------+ This table may contain duplicates (In other words, there is no primary key for this table in SQL). This table contains the ID of the user who sent the request, the ID of the user who received the request, and the date when the request was accepted.
Find the overall acceptance rate of requests, which is the number of acceptance divided by the number of requests. Return the answer rounded to 2 decimals places.
Note that:
friend_request. In this case, Count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate.accept_rate.The result format is in the following example.
Example 1:
Input: FriendRequest table: +-----------+------------+--------------+ | sender_id | send_to_id | request_date | +-----------+------------+--------------+ | 1 | 2 | 2016/06/01 | | 1 | 3 | 2016/06/01 | | 1 | 4 | 2016/06/01 | | 2 | 3 | 2016/06/02 | | 3 | 4 | 2016/06/09 | +-----------+------------+--------------+ RequestAccepted table: +--------------+-------------+-------------+ | requester_id | accepter_id | accept_date | +--------------+-------------+-------------+ | 1 | 2 | 2016/06/03 | | 1 | 3 | 2016/06/08 | | 2 | 3 | 2016/06/08 | | 3 | 4 | 2016/06/09 | | 3 | 4 | 2016/06/10 | +--------------+-------------+-------------+ Output: +-------------+ | accept_rate | +-------------+ | 0.8 | +-------------+ Explanation: There are 4 unique accepted requests, and there are 5 requests in total. So the rate is 0.80.
Follow up:
Problem Overview: You are given two tables: friend_request (all sent requests) and request_accepted (requests that were accepted). The task is to compute the overall acceptance rate, defined as accepted requests / total requests. The result must be rounded to two decimal places, and if there are no requests the answer should return 0.00.
Approach 1: Aggregate Counts with Subqueries (O(n) time, O(1) space)
The most direct solution counts rows in each table and divides them. Use one subquery to count total friend requests and another to count accepted requests. SQL aggregation functions like COUNT() scan the tables once, so the time complexity is O(n), where n is the number of rows across both tables. Wrap the division with ROUND(..., 2) to format the result to two decimal places, and use IFNULL (or COALESCE) to return 0.00 when the denominator is zero. This approach is clean and works well because the problem only asks for a global metric, not per-user statistics.
This technique relies on basic SQL aggregation rather than joins. The database performs two independent table scans and returns a single scalar value. Problems like this commonly appear under Database and SQL practice because they test your ability to combine aggregate functions with conditional handling.
Approach 2: COUNT DISTINCT Pair Aggregation (O(n) time, O(1) space)
Another safe pattern counts unique request pairs from each table using COUNT(DISTINCT ...). In friend_request, count distinct (sender_id, send_to_id). In request_accepted, count distinct (requester_id, accepter_id). This guards against duplicate rows if the dataset contains repeated entries. Divide the accepted pair count by the request pair count and round the result using ROUND. The database still performs linear scans over the tables, giving O(n) time and constant extra memory.
This pattern appears frequently in analytics-style queries where relationships are represented by pairs. Understanding how COUNT(DISTINCT ...) works with composite keys is part of core aggregation query design.
Recommended for interviews: The aggregation approach using COUNT() with ROUND and IFNULL. It shows you understand SQL aggregates, edge-case handling when no rows exist, and how to compute ratios directly inside a query. Interviewers expect a concise one-query solution rather than procedural logic.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Aggregate counts with subqueries | O(n) | O(1) | Best general solution when only total counts are needed |
| COUNT DISTINCT pair aggregation | O(n) | O(1) | When duplicate request rows may exist and unique pairs must be enforced |
FACEBOOK/META LeetCode 597 "Friend Requests I" Interview SQL Question Detailed Explanation • Everyday Data Science • 2,752 views views
Watch 5 more video solutions →Practice Friend Requests I: Overall Acceptance Rate with our built-in code editor and test cases.
Practice on FleetCode