코딩테스트/MySQL
[프로그래머스 Level 4] 식품분류별 가장 비싼 식품의 정보 조회하기 (MySQL)
최-코드
2024. 11. 26. 16:10
https://school.programmers.co.kr/learn/courses/30/lessons/131116
group by를 사용할 때 집계함수를 사용한 컬럼이 있을 경우 실행이 되지 않든가, 정확하지 않은 값이 나올 수 있다.
따라서 서브 쿼리로 따로 빼줘야 한다.
select category, price as max_price, product_name
from food_product
where (category, price) in (select category, max(price) as max_price
from food_product
where category in ('과자', '국','김치','식용유')
group by category)
order by price desc