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

설명
친구 요청을 한 사람이던, 받은 사람이던 상관 없이 친구가 가장 많은 회원의 id과 친구 수를 출력하는 문제이다.
union all 과 서브 쿼리를 사용한다면 어렵지 않은 문제이다.
최댓값을 구해야하므로, 서브쿼리를 가지고 max 값을 구하고, 그 값을 where 절에서 비교해주어야한다.
구현
# Write your MySQL query statement below
with TOTAL_USERS as
( select requester_id as id
from RequestAccepted
union all
select accepter_id as id
from RequestAccepted ),
FRIEND_COUNT as
( select id, count(*) as num
from TOTAL_USERS
group by id )
select *
from FRIEND_COUNT
where num = (select max(num) from FRIEND_COUNT) ;'Coding > SQL' 카테고리의 다른 글
| [LeetCode-SQL 50] 626. Exchange Seats (0) | 2025.03.04 |
|---|---|
| [LeetCode-SQL 50] 585. Investments in 2016 (0) | 2025.03.03 |
| [LeetCode-SQL 50] 1321. Restaurant Growth (0) | 2025.02.27 |
| [LeetCode-SQL 50] 1341. Movie Rating (0) | 2025.02.26 |
| [LeetCode-SQL 50] 626. Exchange Seats (0) | 2025.02.25 |