Compute the Overall Friend Request Acceptance Rate from Distinct Pairs
Company: Home Depot
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
You have a table of friend requests and a table of accepted requests. Return the overall acceptance rate as `accept_rate`: the number of acceptances divided by the number of requests, rounded to 2 decimals.
Count only distinct (sender, receiver) pairs for both requests and acceptances. Count every distinct accepted pair, even when it does not appear in `FriendRequest`. If there are no requests, return `0.00`.
```hint Where the division goes wrong
Count each table on its own. Then check what your query does when the request count is zero, and what PostgreSQL does when it divides one integer by another.
```
### Input Table(s)
`FriendRequest`
| Column | Type | Meaning |
|---|---|---|
| sender_id | INT, NOT NULL | User who sent the request |
| send_to_id | INT, NOT NULL | User who received the request |
| request_date | DATE, NOT NULL | Day the request was sent |
No primary key. The same pair can appear more than once, for example when a request is sent again on a later day.
`RequestAccepted`
| Column | Type | Meaning |
|---|---|---|
| requester_id | INT, NOT NULL | User who sent the original request |
| accepter_id | INT, NOT NULL | User who accepted it |
| accept_date | DATE, NOT NULL | Day the request was accepted |
No primary key. The same pair can appear more than once.
### Output Contract
- One row with one column, `accept_rate`.
- `accept_rate` is the number of distinct `(requester_id, accepter_id)` pairs in `RequestAccepted` divided by the number of distinct `(sender_id, send_to_id)` pairs in `FriendRequest`, rounded to 2 decimal places.
- Return `0.00` when `FriendRequest` has no rows.
- The result always has exactly one row, so no ordering is needed.
### Example
`FriendRequest`
| sender_id | send_to_id | request_date |
|---|---|---|
| 10 | 20 | 2024-03-01 |
| 10 | 20 | 2024-03-04 |
| 10 | 30 | 2024-03-01 |
| 20 | 30 | 2024-03-02 |
| 40 | 10 | 2024-03-05 |
| 30 | 40 | 2024-03-06 |
`RequestAccepted`
| requester_id | accepter_id | accept_date |
|---|---|---|
| 10 | 20 | 2024-03-02 |
| 10 | 20 | 2024-03-05 |
| 20 | 30 | 2024-03-03 |
| 50 | 10 | 2024-03-07 |
Expected result:
| accept_rate |
|---|
| 0.60 |
`FriendRequest` has 5 distinct pairs, because (10, 20) was sent twice. `RequestAccepted` has 3 distinct pairs: (10, 20) counts once, and (50, 10) counts even though no request for it exists. 3 / 5 = 0.60.
### Constraints and Clarifications
- Pairs are ordered: (1, 2) and (2, 1) are two different pairs.
- Dates play no part in the calculation. Two rows with the same pair and different dates count once.
- Accepted pairs without a matching request still count, so the rate can be above 1.
- No column contains NULL.
Overview: Write a SQL query that returns the overall friend request acceptance rate, counting distinct sender and receiver pairs in each table and rounding to two decimals. It checks deduplication, safe division when there are no requests, and numeric casting in PostgreSQL.