Coding Test

602. Friend Requests II: Who Has the Most Friends

Indo Yoon

Problem

LeetCode - The World’s Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

Solution

SELECT Req.id, IFNULL(req_count, 0) + IFNULL(acc_count,0) AS num
FROM
(SELECT requester_id AS id, COUNT(requester_id) AS req_count
FROM RequestAccepted
GROUP BY requester_id) AS Req
LEFT JOIN
(SELECT accepter_id AS id, COUNT(accepter_id) AS acc_count
FROM RequestAccepted
GROUP BY accepter_id) AS Acc
ON Req.id = Acc.id
ORDER BY num DESC
LIMIT 1