[LeetCode/SQL] 602. Friend Requests II: Who Has the Most Friends

Sooyeon·2024년 1월 13일
0

문제풀이 

목록 보기
77/95
post-thumbnail

[LeetCode/SQL]


📌 602. Friend Requests II: Who Has the Most Friends

문제

풀이

가장 많은 친구를 가진 사람과 그 친구의 수를 찾아라 

방법 I

WITH count_id AS (
    SELECT requester_id AS id
    FROM RequestAccepted
    UNION ALL
    SELECT accepter_id AS id
    FROM RequestAccepted

)

SELECT id
       ,COUNT(*) num
FROM count_id 
GROUP BY id
ORDER BY num DESC 
LIMIT 1

방법 II

SELECT m.name
FROM employee as e
INNER JOIN employee as m
ON e.managerId=m.id
GROUP BY e.managerId 
HAVING COUNT(m.id)>=5

0개의 댓글