Interview Questions/SQL/Friend Request Acceptance Rate

Friend Request Acceptance Rate

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given a table of friend request actions. Each row represents either a 'sent' or 'accepted' action between a sender and a receiver. Write a SQL query to find for each sender: the total number of requests sent, the total number accepted, and the acceptance rate (accepted / sent), rounded to 2 decimal places. Senders with zero accepted requests should still appear with an acceptance rate of 0.00. Return the result ordered by sender_id. Note: You can assume that for every 'accepted' row, a corresponding 'sent' row exists for the same sender_id and receiver_id pair. Table: FriendRequests

Column NameTypeDescription
sender_idINTID of the user who sent the request
receiver_idINTID of the user who received it
actionVARCHAR'sent' or 'accepted'
action_dateDATEDate of the action

Database Schema (Inferred)

FriendRequests

Column NameExample Value
sender_id1
receiver_id2
actionsent
action_date2023-01-01

Example

FriendRequests

sender_idreceiver_idactionaction_date
12sent2023-01-01
12accepted2023-01-02
13sent2023-01-03
23sent2023-01-04
23accepted2023-01-05
24sent2023-01-06
34sent2023-01-07

Output

sender_idtotal_senttotal_acceptedacceptance_rate
1210.5
2210.5
3100

Explanation:

Sender 1 sent 2 requests, 1 accepted → rate 0.50. Sender 2 sent 2, 1 accepted → 0.50. Sender 3 sent 1, none accepted → 0.00.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

FriendRequests

sender_idreceiver_idactionaction_date
12sent2023-01-01
12accepted2023-01-02
13sent2023-01-03
23sent2023-01-04
23accepted2023-01-05
24sent2023-01-06
34sent2023-01-07

Output

sender_idtotal_senttotal_acceptedacceptance_rate
1210.5
2210.5
3100