Quick 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.

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.

You have a table of friend requests, `friendrequest`, and a table of accepted requests, `requestaccepted`. `friendrequest` has columns sender_id (INTEGER, NOT NULL, the user who sent the request), send_to_id (INTEGER, NOT NULL, the user who received the request) and request_date (DATE, NOT NULL, the day the request was sent). It has no primary key. The same pair can appear more than once, for example when a request is sent again on a later day. `requestaccepted` has columns requester_id (INTEGER, NOT NULL, the user who sent the original request), accepter_id (INTEGER, NOT NULL, the user who accepted it) and accept_date (DATE, NOT NULL, the day the request was accepted). It has no primary key. The same pair can appear more than once. Return the overall acceptance rate as `accept_rate`: 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. Count only distinct pairs for both requests and acceptances. Count every distinct accepted pair, even when it does not appear in `friendrequest`, so the rate can be above 1. 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. If `friendrequest` has no rows, return 0.00. No column contains NULL. The result is exactly one row with one column, `accept_rate`, so no ordering is needed.

Tables

friendrequest(sender_id INTEGER, send_to_id INTEGER, request_date DATE)

requestaccepted(requester_id INTEGER, accepter_id INTEGER, accept_date DATE)

Hints

  1. 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.
Home DepotData ScientisteasyData Manipulation (SQL/Python)

Friend Request Acceptance Rate

You have a table of friend requests, friendrequest, and a table of accepted requests, requestaccepted.

friendrequest has columns sender_id (INTEGER, NOT NULL, the user who sent the request), send_to_id (INTEGER, NOT NULL, the user who received the request) and request_date (DATE, NOT NULL, the day the request was sent). It has no primary key. The same pair can appear more than once, for example when a request is sent again on a later day.

requestaccepted has columns requester_id (INTEGER, NOT NULL, the user who sent the original request), accepter_id (INTEGER, NOT NULL, the user who accepted it) and accept_date (DATE, NOT NULL, the day the request was accepted). It has no primary key. The same pair can appear more than once.

Return the overall acceptance rate as accept_rate: 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.

Count only distinct pairs for both requests and acceptances. Count every distinct accepted pair, even when it does not appear in friendrequest, so the rate can be above 1. 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. If friendrequest has no rows, return 0.00. No column contains NULL.

The result is exactly one row with one column, accept_rate, so no ordering is needed.

Expected Output

Tables

friendrequestrequestaccepted
friendrequest
ColumnType
sender_idINTEGER
send_to_idINTEGER
request_dateDATE
requestaccepted
ColumnType
requester_idINTEGER
accepter_idINTEGER
accept_dateDATE

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions