Interview Questions/SQL/Friend Request Acceptance Rate

Friend Request Acceptance Rate

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

Medium

Friend Request Acceptance Rate

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

Examples

Example 1

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.

query.sql