https://school.programmers.co.kr/learn/courses/30/lessons/131116
코드설명
INNER JOIN + GROUP BY + MAX + IN + SUBQUERY를 활용합니다.
코드
-- 코드를 입력하세요
SELECT FP.CATEGORY, FP.PRICE, FP.PRODUCT_NAME
FROM FOOD_PRODUCT AS FP
INNER JOIN (SELECT CATEGORY, MAX(PRICE) AS MAX_PRICE FROM FOOD_PRODUCT GROUP BY CATEGORY) AS FP2
ON FP.CATEGORY = FP2.CATEGORY AND FP.PRICE = FP2.MAX_PRICE
WHERE FP.CATEGORY IN ('과자', '국', '김치', '식용유')
ORDER BY FP.PRICE DESC
ORACLE
작성하면서 실수했던 부분은, SQL쿼리문의 순서를 잘못작성했습니다.
SQL은 항상 SELECT FROM JOIN ON WHERE GROUP BY HAVING ORDER BY 와 같은 형태입니다.
하지만 처음에 WHERE절을 GROUP BY 뒤에 작성해서 ORA-00907: missing right parenthesis 에러가 발생했었습니다.
-- 코드를 입력하세요
SELECT FP1.CATEGORY, FP1.PRICE, FP1.PRODUCT_NAME
FROM FOOD_PRODUCT FP1
INNER JOIN (
SELECT CATEGORY, MAX(PRICE) AS PRICE
FROM FOOD_PRODUCT
WHERE REGEXP_LIKE(CATEGORY, '과자|국|김치|식용유')
GROUP BY CATEGORY
) FP2
ON FP1.CATEGORY = FP2.CATEGORY AND FP1.PRICE = FP2.PRICE
ORDER BY FP1.PRICE DESC