Skip to content

Commit e3ece4b

Browse files
authored
Last question from easy set done today
1 parent c456ead commit e3ece4b

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Easy/Friend Requests 1.sql

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Question 49
2+
-- In social network like Facebook or Twitter, people send friend requests and accept others’ requests as well. Now given two tables as below:
3+
4+
5+
-- Table: friend_request
6+
-- | sender_id | send_to_id |request_date|
7+
-- |-----------|------------|------------|
8+
-- | 1 | 2 | 2016_06-01 |
9+
-- | 1 | 3 | 2016_06-01 |
10+
-- | 1 | 4 | 2016_06-01 |
11+
-- | 2 | 3 | 2016_06-02 |
12+
-- | 3 | 4 | 2016-06-09 |
13+
14+
15+
-- Table: request_accepted
16+
-- | requester_id | accepter_id |accept_date |
17+
-- |--------------|-------------|------------|
18+
-- | 1 | 2 | 2016_06-03 |
19+
-- | 1 | 3 | 2016-06-08 |
20+
-- | 2 | 3 | 2016-06-08 |
21+
-- | 3 | 4 | 2016-06-09 |
22+
-- | 3 | 4 | 2016-06-10 |
23+
24+
25+
-- Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests.
26+
27+
28+
-- For the sample data above, your query should return the following result.
29+
30+
31+
-- |accept_rate|
32+
-- |-----------|
33+
-- | 0.80|
34+
35+
36+
-- Note:
37+
-- The accepted requests are not necessarily from the table friend_request. In this case, you just need to simply count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate.
38+
-- It is possible that a sender sends multiple requests to the same receiver, and a request could be accepted more than once. In this case, the ‘duplicated’ requests or acceptances are only counted once.
39+
-- If there is no requests at all, you should return 0.00 as the accept_rate.
40+
41+
42+
-- Explanation: There are 4 unique accepted requests, and there are 5 requests in total.
43+
-- So the rate is 0.80.
44+
45+
-- Solution
46+
with t1 as
47+
(
48+
select distinct sender_id, send_to_id
49+
from friend_request
50+
), t2 as
51+
(
52+
select distinct requester_id, accepter_id
53+
from request_accepted
54+
)
55+
56+
Select
57+
ifnull((
58+
select distinct
59+
round((select count(*) from t2) / ( select count(*) from t1),2) from t1,t2
60+
),0) 'accept_rate'

0 commit comments

Comments
 (0)