-- 코드를 입력하세요
SELECT I.REST_ID, I.REST_NAME, I.FOOD_TYPE, I.FAVORITES, I.ADDRESS, ROUND(AVG(R.REVIEW_SCORE),2) as SCORE
FROM REST_INFO I
INNER JOIN(
SELECT REST_ID, REVIEW_SCORE
FROM REST_REVIEW
) AS R on I.REST_ID = R.REST_ID
WHERE I.ADDRESS LIKE "서울%"
GROUP BY REST_ID
ORDER BY SCORE DESC, FAVORITES DESC
-- 코드를 입력하세요
SELECT f.PRODUCT_ID, f.PRODUCT_NAME, f.PRODUCT_CD, f.CATEGORY, f.PRICE
FROM FOOD_PRODUCT f
INNER JOIN(
SELECT MAX(PRICE) AS PRICE
FROM FOOD_PRODUCT
) AS m ON f.PRICE= m.PRICE;
-- 코드를 입력하세요
SELECT c.CAR_ID
FROM CAR_RENTAL_COMPANY_CAR c
INNER JOIN(
SELECT CAR_ID, START_DATE
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
) as h on c.CAR_ID = h.CAR_ID
WHERE MONTH(h.START_DATE) = 10 AND c.CAR_TYPE = '세단'
GROUP BY CAR_ID
ORDER BY CAR_ID DESC;
1] CAR_RENTAL_COMPANY_CAR와 CAR_RENTAL_COMPANY_RENTAL_HISTORY 를 CAR_ID 기준으로 INNER JOIN
2] MONTH(h.START_DATE) 을 통해 10월에 대여를 시작한 차의 정보를 얻는다.
3] GROUP BY CAR_ID를 사용하면 문제 조건에 따라 중복없이 값을 출력할 수 있다.
-- 코드를 입력하세요
SELECT b.BOOK_ID, A.AUTHOR_NAME, DATE_FORMAT(b.PUBLISHED_DATE, "%Y-%m-%d")
FROM BOOK b
INNER JOIN(
SELECT AUTHOR_ID, AUTHOR_NAME
FROM AUTHOR
) AS A ON b.AUTHOR_ID = A.AUTHOR_ID
WHERE CATEGORY = '경제'
ORDER BY b.PUBLISHED_DATE;
1] BOOK 테이블에서 BOOK_ID, PUBLISHED_DATE 가져오기
2] DATE_FORMAT(b.PUBLISHED_DATE, "%Y-%m-%d")을 사용하여 형식 맞추기
SELECT FOOD_TYPE, MAX(FAVORITES) AS MaxFavorites
FROM REST_INFO
GROUP BY FOOD_TYPE
(1) FROM REST_INFO
(2) GROUP BY FOOD_TYPE
2] 기존 테이블과 INNER JOIN
-- 코드를 입력하세요
SELECT r.FOOD_TYPE, r.REST_ID, r.REST_NAME, r.FAVORITES
FROM REST_INFO r
INNER JOIN(
SELECT FOOD_TYPE, MAX(FAVORITES) AS MaxFavorites
FROM REST_INFO
GROUP BY FOOD_TYPE
) AS max_res ON r.FOOD_TYPE = max_res.FOOD_TYPE
AND r.FAVORITES = max_res.MaxFavorites
ORDER BY r.FOOD_TYPE DESC;