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

sehyunny·2023년 5월 26일
0

코딩테스트

목록 보기
5/11

https://school.programmers.co.kr/learn/courses/30/lessons/131123


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

WITH fav AS (
    SELECT food_type,
           rest_name,
           MAX(favorites) OVER (PARTITION BY food_type) AS favorites
    FROM rest_info)
    
SELECT DISTINCT rest_info.food_type,
       rest_info.rest_id,
       rest_info.rest_name,
       rest_info.favorites
FROM rest_info
     INNER JOIN fav ON rest_info.food_type = fav.food_type
     AND rest_info.favorites = fav.favorites
ORDER BY rest_info.food_type DESC

0개의 댓글