Friend Requests I: Overall Acceptance Rate - Problem

You are given two tables representing a social network's friend request system.

Table: FriendRequest

  • sender_id: ID of the user who sent the request
  • send_to_id: ID of the user who received the request
  • request_date: Date when the request was sent

Table: RequestAccepted

  • requester_id: ID of the user who sent the request
  • accepter_id: ID of the user who received the request
  • accept_date: Date when the request was accepted

Find the overall acceptance rate of friend requests, calculated as the number of acceptances divided by the number of requests. Return the result rounded to 2 decimal places.

Important Notes:

  • Both tables may contain duplicates - count unique request/acceptance pairs only
  • Accepted requests don't need to exist in the FriendRequest table
  • If there are no requests, return 0.00

Table Schema

FriendRequest
Column Name Type Description
sender_id int ID of user sending friend request
send_to_id int ID of user receiving friend request
request_date date Date when request was sent
Primary Key: None (may contain duplicates)
Note: Contains friend request data, may have duplicate entries
RequestAccepted
Column Name Type Description
requester_id int ID of user who sent the accepted request
accepter_id int ID of user who accepted the request
accept_date date Date when request was accepted
Primary Key: None (may contain duplicates)
Note: Contains accepted request data, may have duplicate entries

Input & Output

Example 1 — Basic Acceptance Rate
Input Tables:
FriendRequest
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
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
Output:
accept_rate
0.80
💡 Note:

There are 5 distinct friend requests and 4 distinct acceptances. The acceptance rate is 4/5 = 0.80 or 80%.

Example 2 — No Requests Edge Case
Input Tables:
FriendRequest
sender_id send_to_id request_date
RequestAccepted
requester_id accepter_id accept_date
1 2 2016-06-03
Output:
accept_rate
0.00
💡 Note:

When there are no friend requests, the acceptance rate should be 0.00 even if there are some acceptances recorded.

Example 3 — Handling Duplicates
Input Tables:
FriendRequest
sender_id send_to_id request_date
1 2 2016-06-01
1 2 2016-06-02
RequestAccepted
requester_id accepter_id accept_date
1 2 2016-06-03
1 2 2016-06-04
Output:
accept_rate
1.00
💡 Note:

Even though there are duplicate entries, we count only 1 distinct request (1→2) and 1 distinct acceptance (1→2), giving a rate of 1/1 = 1.00.

Constraints

  • 1 ≤ sender_id, send_to_id ≤ 1000
  • 1 ≤ requester_id, accepter_id ≤ 1000
  • Tables may contain duplicate records
  • Result should be rounded to 2 decimal places

Visualization

Tap to expand
Friend Request Acceptance Rate Problem OverviewInput TablesFriendRequest5 requests(with duplicates)RequestAccepted4 acceptances(with duplicates)SQL QueryProcessing1. COUNT DISTINCT requests2. COUNT DISTINCT accepts3. DIVIDE with ROUND()4/5 = 0.80Outputaccept_rate0.80Key Challenge: Handle Duplicates• Use COUNT(DISTINCT sender_id, send_to_id) to avoid duplicate requests• Use COUNT(DISTINCT requester_id, accepter_id) for unique acceptances• Handle division by zero with COALESCE and NULLIF
Understanding the Visualization
1
Count Requests
Count distinct friend request pairs
2
Count Acceptances
Count distinct acceptance pairs
3
Calculate Rate
Divide acceptances by requests with proper rounding
Key Takeaway
🎯 Key Insight: Always use DISTINCT when counting pairs to handle duplicate records properly
Asked in
Facebook 25 LinkedIn 18
28.0K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen