Friend Request Acceptance Rate
Preview mode. Log in to edit, run, submit, and save progress.
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 Name | Type | Description |
|---|---|---|
| sender_id | INT | ID of the user who sent the request |
| receiver_id | INT | ID of the user who received it |
| action | VARCHAR | 'sent' or 'accepted' |
| action_date | DATE | Date of the action |
Database Schema (Inferred)
FriendRequests
| Column Name | Example Value |
|---|---|
| sender_id | 1 |
| receiver_id | 2 |
| action | sent |
| action_date | 2023-01-01 |
Example
FriendRequests
| sender_id | receiver_id | action | action_date |
|---|---|---|---|
| 1 | 2 | sent | 2023-01-01 |
| 1 | 2 | accepted | 2023-01-02 |
| 1 | 3 | sent | 2023-01-03 |
| 2 | 3 | sent | 2023-01-04 |
| 2 | 3 | accepted | 2023-01-05 |
| 2 | 4 | sent | 2023-01-06 |
| 3 | 4 | sent | 2023-01-07 |
Output
| sender_id | total_sent | total_accepted | acceptance_rate |
|---|---|---|---|
| 1 | 2 | 1 | 0.5 |
| 2 | 2 | 1 | 0.5 |
| 3 | 1 | 0 | 0 |
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.
FriendRequests
| sender_id | receiver_id | action | action_date |
|---|---|---|---|
| 1 | 2 | sent | 2023-01-01 |
| 1 | 2 | accepted | 2023-01-02 |
| 1 | 3 | sent | 2023-01-03 |
| 2 | 3 | sent | 2023-01-04 |
| 2 | 3 | accepted | 2023-01-05 |
| 2 | 4 | sent | 2023-01-06 |
| 3 | 4 | sent | 2023-01-07 |
Output
| sender_id | total_sent | total_accepted | acceptance_rate |
|---|---|---|---|
| 1 | 2 | 1 | 0.5 |
| 2 | 2 | 1 | 0.5 |
| 3 | 1 | 0 | 0 |