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

+ Recent posts