[MySQL] 즐겨찾기가 가장 많은 식당 정보 출력하기

yejichoi·2023년 3월 15일
0

Database

목록 보기
31/31
post-thumbnail

즐겨찾기가 가장 많은 식당 정보 출력하기

REST_INFO 테이블에서 음식종류별로 즐겨찾기수가 가장 많은 식당의 음식 종류, ID, 식당 이름, 즐겨찾기수를 조회하는 SQL문을 작성해주세요. 이때 결과는 음식 종류를 기준으로 내림차순 정렬해주세요.

예시

출력


나의 풀이

지금와서 생각해보니 즐겨찾기수를 조작하는거면 having이 아니라 where을 썻어야 했네... 이걸 지금 블로깅 하면서 생각했네😳

 SELECT food_type, rest_id, rest_name, max(favorites)
 from rest_info 
 #where
 group by food_type
 having max(favorites) limit 1 # max와 limit은 같은 말인데
 order by food_type desc

✂️ 틀린 풀이

# having
SELECT food_type,rest_id,rest_name,favorites
from rest_info
group by food_type
having (max(favorites))
order by food_type desc

🥨 정답

#where #서브쿼리 
SELECT food_type,rest_id,rest_name,favorites from rest_info
where favorites in (select max(favorites)  from rest_info
                   group by food_type)
group by food_type
order by food_type desc

Group by - 내부적인 작동 옵션

서브쿼리 참고 블로그

0개의 댓글