97. Social influencer

easy facebook

  • ************************************************************
  • *******************************************************************************************
  • *****************************************

Table: accepted_request

Log when a friend request is accepted

  col_name      | col_type
----------------+------------------------
 sender_id      | bigint
 recipient_id   | bigint
 acceptance_dt  |  date

Sample results

id
--------------
1000020

Solution postgres

SELECT user_id_1 AS user_id
FROM (
	SELECT sender_id AS user_id_1, recipient_id user_id_2
	FROM accepted_request
	UNION 
	SELECT recipient_id AS user_id_1, sender_id user_id_2
	FROM accepted_request
) X
GROUP BY 1
ORDER BY COUNT(DISTINCT user_id_2) DESC
LIMIT 1;
    

Explanation

This query selects a user (aliasing it as "user_id") who has the most distinct accepted requests with other users.

To do this, it starts by creating a subquery that combines the sender and recipient IDs from the "accepted_request" table. It does this twice, once for each direction of the request (sender to recipient and recipient to sender). This is done using the UNION operator.

The resulting table is then grouped by the "user_id_1" column (which contains the user ID from the first column of the subquery). It counts the distinct "user_id_2" values (which contains the user ID from the second column of the subquery) for each "user_id_1". The results are then sorted in descending order based on the count of distinct "user_id_2" values.

Finally, the query selects only the first row (using LIMIT 1), which will be the user with the most distinct accepted requests with other users.

Expected results


More Facebook questions

ID Title Level FTPR
81 How many people searched on new year's day facebook easy
24%
82 The top search query on new year's day facebook easy
13%
83 Top search_query in US and UK on new year's day facebook medium
6%
84 Click through rate on new year's day facebook medium
9%
85 Top 4 queries based on click through rate on new year's day facebook hard
7%
96 Overall acceptance rate facebook easy
13%
98 Most popular user facebook easy
33%
99 Page recommendation facebook medium
13%
100 Advertiser ROI facebook medium
8%
172 Comments distribution facebook hard
13%
173 User Popularity Percentage facebook medium
11%
174 Closed accounts facebook easy
23%