LeetCode - 602. Friend Requests II: Who Has the Most Friends (MySQL)

조민수·2024년 6월 5일
0

LeetCode

목록 보기
15/64
post-custom-banner

Medium, SQL - UNION ALL

RunTime : 286 ms


문제

Write a solution to find the people who have the most friends and the most friends number.

The test cases are generated so that only one person has the most friends.

The result format is in the following example.


풀이

  • UNION, UNION ALL이 아직은 친숙하지 않아서 헤맸다.
  • UNION ALL은 모든 중복값을 그대로 출력해준다.
    • 출력값들 중에서 개수를 세서 LIMIT 1을 통해 제일 첫 번째를 출력한다.
SELECT id, COUNT(*) AS num
FROM (
    SELECT requester_id AS id FROM RequestAccepted
    UNION ALL
    SELECT accepter_id AS id FROM RequestAccepted
) AS FC
GROUP BY id
ORDER BY num DESC
LIMIT 1
profile
사람을 좋아하는 Front-End 개발자
post-custom-banner

0개의 댓글