You are given a table RequestAccepted that tracks accepted friend requests between users.
Table: RequestAccepted
| Column Name | Type |
|---|---|
| requester_id | int |
| accepter_id | int |
| accept_date | date |
The primary key is (requester_id, accepter_id). This table contains the ID of the user who sent the request, the ID of the user who received the request, and the date when the request was accepted.
Task: Write a SQL solution to find the person who has the most friends and return their ID along with the total number of friends.
The test cases guarantee that only one person has the maximum number of friends.
Table Schema
| Column Name | Type | Description |
|---|---|---|
requester_id
PK
|
int | ID of user who sent the friend request |
accepter_id
PK
|
int | ID of user who accepted the friend request |
accept_date
|
date | Date when the request was accepted |
Input & Output
| 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 |
| id | num |
|---|---|
| 3 | 3 |
User 3 has the most friends (3 total). User 3 appears as: accepter when user 1 sent request, accepter when user 2 sent request, and requester when sending to user 4. This gives 3 total friend connections.
| requester_id | accepter_id | accept_date |
|---|---|---|
| 1 | 2 | 2016-06-03 |
| 3 | 4 | 2016-06-08 |
| id | num |
|---|---|
| 1 | 1 |
All users have exactly 1 friend each. The query returns user 1 with 1 friend (user 2). Each friendship creates 2 entries in our UNION result, so each person gets counted once.
Constraints
-
1 ≤ requester_id, accepter_id ≤ 10000 -
requester_id ≠ accepter_id - All friend requests in the table are accepted
- Test cases guarantee exactly one person has the maximum friends