Friend Requests II: Who Has Most Friend?


  • 0

    Click here to see the full article post


  • 0
    L

    FIRST TWO SELECT SHOULD BE COMBINED TO ONE SELECT


  • 0
    L

    The outer select is redundant, can just do
    select id, count(1) as num
    from (
    select requester_id as id from request_accepted
    union all
    select accepter_id as id from request_accepted
    ) t
    group by 1
    order by 2 desc
    limit 1
    ;


  • 0
    C

    Other way - SQL

    select top 1 c.nfriends, id from
    (select id, count(*) as nfriends from
    (select accepterid as id
    from friend
    union all
    select requesterid
    from friend) as a
    group by id) c
    order by nfriends desc


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.