Find the Person with the Most Friends from Accepted Friend Requests

Quick Overview

Write a SQL query that finds the person with the most friends in a table of accepted friend requests and returns their id and friend count. It checks whether you count a person on both sides of a friendship and aggregate the combined ids correctly.

Find the Person with the Most Friends from Accepted Friend Requests

Company: Shopify

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Each row in `RequestAccepted` is an accepted friendship between `requester_id` and `accepter_id` (both person ids). Return the person with the most friends as `id` and their friend count `num`. ```hint Both sides of a row A friendship belongs to both people in the row. Make sure a person's count includes the rows where they sent the request and the rows where they accepted one. ``` ### Input Table(s) `RequestAccepted` | Column | Type | Meaning | |---|---|---| | requester_id | INT, NOT NULL | Person who sent the friend request | | accepter_id | INT, NOT NULL | Person who accepted it | | accept_date | DATE, nullable | Day the request was accepted | Key: `(requester_id, accepter_id)` is unique. ### Output Contract - Exactly one row with columns `id` and `num`. - `id` is the person with the most friends. - `num` is that person's friend count: the number of rows in which they appear, as `requester_id` or as `accepter_id`. - The input guarantees a single winner, so the result has one row and needs no further ordering. ### Example `RequestAccepted` | 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 | Expected result: | id | num | |---|---| | 3 | 3 | Person 3 appears as accepter in rows (1, 3) and (2, 3) and as requester in row (3, 4), giving 3 friends total. Person 1 has 2 friends, person 2 has 2 friends, and person 4 has 1 friend. So person 3 has the most friends. ### Constraints and Clarifications - At most 50,000 rows. - The answer is unique (no ties). - `requester_id != accepter_id` for every row. - Each `(requester_id, accepter_id)` pair is unique, and each friendship is stored in one row only, so the number of rows a person appears in is their number of friends. - `accept_date` can be NULL. It does not affect the count.

Overview: Write a SQL query that finds the person with the most friends in a table of accepted friend requests and returns their id and friend count. It checks whether you count a person on both sides of a friendship and aggregate the combined ids correctly.

Shopify logo
Shopify
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row in RequestAccepted is an accepted friendship between requester_id and accepter_id (both person ids). Return the person with the most friends as id and their friend count num.

Input Table(s)

RequestAccepted

ColumnTypeMeaning
requester_idINT, NOT NULLPerson who sent the friend request
accepter_idINT, NOT NULLPerson who accepted it
accept_dateDATE, nullableDay the request was accepted

Key: (requester_id, accepter_id) is unique.

Output Contract

  • Exactly one row with columns id and num .
  • id is the person with the most friends.
  • num is that person's friend count: the number of rows in which they appear, as requester_id or as accepter_id .
  • The input guarantees a single winner, so the result has one row and needs no further ordering.

Example

RequestAccepted

requester_idaccepter_idaccept_date
122016-06-03
132016-06-08
232016-06-08
342016-06-09

Expected result:

idnum
33

Person 3 appears as accepter in rows (1, 3) and (2, 3) and as requester in row (3, 4), giving 3 friends total. Person 1 has 2 friends, person 2 has 2 friends, and person 4 has 1 friend. So person 3 has the most friends.

Constraints and Clarifications

  • At most 50,000 rows.
  • The answer is unique (no ties).
  • requester_id != accepter_id for every row.
  • Each (requester_id, accepter_id) pair is unique, and each friendship is stored in one row only, so the number of rows a person appears in is their number of friends.
  • accept_date can be NULL. It does not affect the count.