프로그래머스 - 즐겨찾기가 가장 많은 식당 정보 출력하기 mySQL

.·2022년 10월 8일
0
-- 코드를 입력하세요
SELECT a.food_type, a.rest_id, a.rest_name, a.favorites
from rest_info as a
inner join (
select food_type, max(favorites) as res
    from rest_info
    group by food_type
) as b 
on b.food_type = a.food_type
and a.favorites = b.res
order by a.food_type desc

풀이

  • 컬럼별 데이터가 필요 할때는 group by 절을 사용해야 한다.
  • 셀프조인이 필요 했다.
    - 왜? : 다이렉트로 max(a.favorites)를 뽑아내면 원하는 값이 나오더라도, 나머지 컬럼(a.rest_id , a.rest_name)의 값이 다를 수가 있기 때문이다.
profile
.

0개의 댓글