서브쿼리 : 메인 SELECT 구문 안에 다른 SELECT문 ▷ 조건문의 형태로 이해하자
SELECT
PRODUCT_ID,
PRODUCT_NAME,
LIST_PRICE
FROM PRODUCTS
WHERE LIST_PRICE = (SELECT
MAX(LIST_PRICE)
FROM PRODUCTS);
SELECT
A.PRODUCT_NAME,
A.LIST_PRICE,
(
SELECT
AVG(LIST_PRICE)
FROM PRODUCTS B
WHERE B.CATEGORY_ID = A.CATEGORY_ID
) AVG_LIST_PRICE
FROM PRODUCTS A
ORDER BY A.PRODUCT_NAME;
위와같이 SELECT절 안의 서브쿼리를 스칼라 서브쿼리라고 한다.
SELECT
ORDER_ID,
ORDER_VALUE
FROM
(
SELECT
ORDER_ID,
SUM(QUANTITY * UNIT_PRICE) ORDER_VALUE
FROM
ORDER_ITEMS
GROUP BY ORDER_ID
ORDER BY ORDER_VALUE DESC
)
WHERE
ROWNUM <= 10 ;
FROM절 안에 있는 SELECT문을 통해 VIEW가 생성됨 -> VIEW에서 ORDER_ID와 ORDER_VALUE중 10개만 표시함 (인라인 뷰 서브쿼리)
SELECT
A.PRODUCT_ID ,
A.PRODUCT_NAME ,
A.LIST_PRICE
FROM PRODUCTS A
WHERE LIST_PRICE > (SELECT
AVG(K.LIST_PRICE)
FROM PRODUCTS K)
ORDER BY PRODUCT_NAME ;
SELECT
*
FROM (SELECT *
FROM CUSTOMERS
ORDER BY CREDIT_LIMIT DESC, NAME)
WHERE ROWNUM <= 10;