프로그래밍연습/SQL

[SQL]friend-requests-ii-who-has-the-most-friends

Q_jihe 2023. 8. 20. 13:47

https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends

 

Friend Requests II: Who Has the Most Friends - LeetCode

Can you solve this real interview question? Friend Requests II: Who Has the Most Friends - Table: RequestAccepted +----------------+---------+ | Column Name | Type | +----------------+---------+ | requester_id | int | | accepter_id | int | | accept_date |

leetcode.com

with base as (
    select
        requester_id as id
        , count(distinct accepter_id) cnt
    from RequestAccepted
    group by 1
    union all
    select
        accepter_id as id
        , count(distinct requester_id) cnt
    from RequestAccepted
    group by 1
)
, total as (
    select 
        id, sum(cnt) as num
    from base
    group by 1
)
select
    distinct id, num
from total
order by num desc
limit 1