https://school.programmers.co.kr/learn/courses/30/lessons/131123
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
생각 흐름
- group by에 사용되지 않은 컬럼들은 보통 순서의 맨 위의 컬럼 값을 가져온다.
- 따라서 main 쿼리 절에서 group by를 사용하면 안 된다.
- 서브쿼리 절로 food_type과 favorites의 max 값을 가져오고 메인 쿼리와 food_type과 favorites에 대해 inner join 시킨다.
select a.food_type, a.rest_id, a.rest_name, a.favorites
from rest_info a inner join (
select food_type, max(favorites) as favorites
from rest_info
group by food_type
) as b
on a.food_type = b.food_type and a.favorites = b.favorites
order by a.food_type desc
'코딩테스트 > MySQL' 카테고리의 다른 글
[ 프로그래머스 Level 3 ] 즐겨찾기가 가장 많은 식당 정보 출력하기 (MySQL) (0) | 2025.03.13 |
---|---|
[ 프로그래머스 Level 3 ] 대여 기록이 존재하는 자동차 리스트 구하기 (MySQL) (0) | 2025.03.09 |
[ 프로그래머스 Level 3 ] 조건에 맞는 사용자와 총 거래금액 조회하기 (MySQL) (0) | 2025.03.07 |
[프로그래머스 Level 5] 상품을 구매한 회원 비율 구하기 (MySQL) (0) | 2025.03.06 |
[프로그래머스 Level 5] 멸종위기의 대장균 찾기 (MySQL) (0) | 2025.03.04 |