본문 바로가기

DB/Programmers SQL Kit

서울에 위치한 식당 목록 출력하기

https://school.programmers.co.kr/learn/courses/30/lessons/131118?language=oracle

- rest_info 테이블의 id, name, food_type, favorites, address와 rest_review 테이블의 review_score의 평균 점수를 출력해야한다.

단, 주소가 서울이어야하며 평균 점수는 3번째 자리에서 반올림한다.

1. rest_view 테이블에서 rest_id 별로 그룹화한뒤 Score를 평균(AVG)낸다.

2 평균 낸 테이블과 rest_info 테이블을 rest_id를 기준으로 join한다.

3. join한 테이블에 address가 서울이 들어가는 튜플만 뽑아서 id, name, food_type, favorites, address, SCORE를 조회한다.

4. 조회한 결과물을 평균점수 기준 내림차순, 즐겨찾기수 기준 내림차순으로 정렬한다.

 

- SQL

SELECT ri.rest_id, ri.rest_name, ri.food_type, ri.favorites, ri.address, rr.SCORE

FROM REST_INFO ri, 
    (SELECT rest_id, round(AVG(review_score), 2) SCORE
    FROM REST_REVIEW
    GROUP BY rest_id) rr

WHERE ri.rest_id = rr.rest_id AND ri.address LIKE '서울%'

ORDER BY SCORE DESC, ri.favorites DESC;