Friend Requests II: Who Has the Most Friends - Problem

๐Ÿค Friend Requests II: Who Has the Most Friends

You're working on a social media platform's analytics team! Given a database table RequestAccepted that tracks all accepted friend requests, you need to find the person with the most friends.

๐Ÿ“Š Table Structure:

Column NameTypeDescription
requester_idintID of user who sent the request
accepter_idintID of user who accepted the request
accept_datedateWhen the request was accepted

Key Points:

  • Each row represents a bidirectional friendship (if A sends to B and B accepts, both A and B are friends)
  • The combination (requester_id, accepter_id) is unique
  • You need to return both the person ID and their total friend count
  • There's guaranteed to be exactly one person with the maximum friends

Goal: Write a SQL query to find who has the most friends and how many friends they have.

Input & Output

example_1.sql โ€” Basic Friend Network
$ Input: RequestAccepted table: | requester_id | accepter_id | accept_date | |--------------|-------------|-------------| | 1 | 2 | 2016-06-03 | | 1 | 3 | 2016-06-08 | | 2 | 3 | 2016-06-08 | | 3 | 4 | 2016-06-09 |
โ€บ Output: | id | num | |----|-----| | 3 | 3 |
๐Ÿ’ก Note: User 3 has the most friends. They are connected to users 1, 2, and 4. User 3 appears in 3 friendship records: as accepter with users 1 and 2, and as requester with user 4.
example_2.sql โ€” Single Friendship
$ Input: RequestAccepted table: | requester_id | accepter_id | accept_date | |--------------|-------------|-------------| | 1 | 2 | 2016-06-03 |
โ€บ Output: | id | num | |----|-----| | 1 | 1 | | 2 | 1 |
๐Ÿ’ก Note: Both users have exactly 1 friend each. Either could be returned as they're tied, but the problem guarantees only one person has the maximum.
example_3.sql โ€” Linear Chain
$ Input: RequestAccepted table: | requester_id | accepter_id | accept_date | |--------------|-------------|-------------| | 1 | 2 | 2016-06-03 | | 2 | 3 | 2016-06-04 | | 3 | 4 | 2016-06-05 | | 4 | 5 | 2016-06-06 |
โ€บ Output: | id | num | |----|-----| | 2 | 2 | | 3 | 2 | | 4 | 2 |
๐Ÿ’ก Note: In a linear chain, users 2, 3, and 4 each have 2 friends (their neighbors), while users 1 and 5 have only 1 friend each.

Constraints

  • 1 โ‰ค number of accepted requests โ‰ค 500
  • 1 โ‰ค requester_id, accepter_id โ‰ค 500
  • requester_id โ‰  accepter_id (no self-friend requests)
  • All dates are valid
  • Guaranteed exactly one person has the maximum number of friends

Visualization

Tap to expand
๐Ÿค Friend Network Analysis12342 friends2 friends3 friends โญ1 friendFriendship CountUser 1: appears 2 timesUser 2: appears 2 timesUser 3: appears 3 times โญUser 4: appears 1 timeUNION ALL combines:requester_id + accepter_idThen GROUP BY counts eachSQL: SELECT id, COUNT(*) FROM (SELECT requester_id UNION ALL SELECT accepter_id) GROUP BY id
Understanding the Visualization
1
Collect All Relationships
Each accepted request creates a bidirectional friendship - both requester and accepter gain a friend
2
Count Per Person
Use UNION ALL to list each person every time they appear in a friendship, then group and count
3
Find the Winner
Sort by friend count descending and pick the top person
Key Takeaway
๐ŸŽฏ Key Insight: Friendships are bidirectional, so we count each person's appearances in both requester and accepter roles using UNION ALL, then group and count efficiently.
Asked in
Facebook 45 LinkedIn 38 Instagram 25 Twitter 20
42.0K Views
High Frequency
~12 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen