Skip to content

1951. All the Pairs With the Maximum Number of Common Followers 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
WITH
  RankedRelations AS (
    SELECT
      User1.user_id AS user1_id,
      User2.user_id AS user2_id,
      RANK() OVER(ORDER BY COUNT(User1.follower_id) DESC) AS `rank`
    FROM Relations AS User1
    INNER JOIN Relations AS User2
      USING (follower_id)
    WHERE User1.user_id < User2.user_id
    GROUP BY 1, 2
  )
SELECT user1_id, user2_id
FROM RankedRelations
WHERE `rank` = 1;