Friend Requests II: Who Has the Most Friends

4pts
Meta

Problem Statement

Meta needs to identify the most "social" users on the platform. In the request_accepted table, every row represents a friendship formed between requester_id and accepter_id.

Write an SQL query to find the person who has the most friends and the most friends count.

Rules:

  • A person is a friend if they either requested the friendship or accepted it.
  • The result should contain the id and the total number of friends (num).
  • If there are multiple people with the same maximum number of friends, you only need to report one of them.
  • Results must be sorted by num in descending order (implicit via LIMIT).
Tests your understanding of
Basic SQL, UNION ALL, Aggregation and LIMIT

Input Tables

request_accepted
requester_id(INTEGER)accepter_id(INTEGER)accept_date(DATE)
122016-06-01
132016-06-08
232016-06-08
342016-06-09

Expected Output

id(INTEGER)num(INTEGER)
33

Tags

MediumBasic SQLUNION ALLAggregationLIMIT
15-20 min
58%

Hints