[SQL] 2-1. 쿼리 안에 서브쿼리

chxxrin·2024년 4월 1일
0

sql

목록 보기
3/5

1. 비상관 서브쿼리

원래 쿼리와 안의 쿼리와는 완전 상관없이 돌아가는 것

SELECT
  CategoryID, CategoryName, Description,
  (SELECT ProductName 
   FROM Products 
   WHERE ProductID = 1)
FROM Categories;
ategoryIDCategoryNameDescription(SELECT ProductName FROM Products WHERE ProductID = 1)
1BeveragesSoft drinks, coffees, teas, beers, and alesChais
2CondimentsSweet and savory sauces, relishes, spreads, and seasoningsChais

→ 사실 이거는 의미는 없음

SELECT * 
FROM Products
WHERE Price < (
  SELECT AVG(Price) 
  FROM Products
);
ProductIDProductNameSupplierIDCategoryIDUnitPrice
1Chais1110 boxes x 20 bags18.00
2Chang1124 - 12 oz bottles19.00
SELECT
  CategoryID, CategoryName, Description
FROM Categories
WHERE
  CategoryID =
  (SELECT CategoryID 
   FROM Products
   WHERE ProductName = 'Chais');
CategoryIDCategoryNameDescription
1BeveragesSoft drinks, coffees, teas, beers, and ales
SELECT
  CategoryID, CategoryName, Description
FROM Categories
WHERE
  CategoryID IN
  (SELECT CategoryID 
   FROM Products
   WHERE Price > 50);
CategoryIDCategoryNameDescription
1BeveragesSoft drinks, coffees, teas, beers, and ales
3ConfectionsDesserts, candies, and sweet breads
4Dairy ProductsCheeses
연산자의미
ALL서브쿼리의 모든 결과에 대해 ~하다
ANY서브쿼리의 하나 이상의 결과에 대해 ~하다
SELECT * 
FROM Products
WHERE Price > ALL (
  SELECT Price 
  FROM Products
  WHERE CategoryID = 2
);
ProductIDProductNameSupplierIDCategoryIDUnitPrice
9Mishi Kobe Niku4618 - 500 g pkgs.97.00
18Carnarvon Tigers7816 kg pkg.62.50
20Sir Rodney's Marmalade8330 gift boxes81.00
SELECT
  CategoryID, CategoryName, Description
FROM Categories
WHERE
  CategoryID = ANY
  (SELECT CategoryID 
  FROM Products
  WHERE Price > 50);
SELECT
  CategoryID, CategoryName, Description
FROM Categories
WHERE
  CategoryID IN
  (SELECT CategoryID 
  FROM Products
  WHERE Price > 50);

→ = ANY를 IN 으로 바꿔도 똑같은 결과가 나옴

CategoryIDCategoryNameDescription
1BeveragesSoft drinks, coffees, teas, beers, and ales
3ConfectionsDesserts, candies, and sweet breads
4Dairy ProductsCheeses

2. 상관 서브쿼리

바깥 쿼리와 안쪽 쿼리가 같이 맞물려져서 돌아감

그래서 상관된 테이블들의 닉네임을 만들어줘야함!

SELECT
  ProductID, ProductName,
  (
    SELECT CategoryName 
    FROM Categories C
    WHERE C.CategoryID = P.CategoryID
  ) AS CategoryName
FROM Products P;
ProductIDProductNameCategoryName
1ChaisBeverages
2ChangBeverages
3Aniseed SyrupCondiments
SELECT
  SupplierName, Country, City,
  (
    SELECT COUNT(*) 
    FROM Customers C
    WHERE C.Country = S.Country
  ) AS CustomersInTheCountry,
  (
    SELECT COUNT(*) 
    FROM Customers C
    WHERE C.Country = S.Country 
      AND C.City = S.City
  ) AS CustomersInTheCity
FROM Suppliers S;
SupplierNameCountryCityCustomersInTheCountryCustomersInTheCity
Exotic LiquidUKLondona70
New Orleans Cajun DelightsUSANew Orleans130
Grandma Kelly's HomesteadUSAAnn Arbor13
SELECT
  CategoryID, CategoryName,
  (
    SELECT MAX(Price) 
    FROM Products P
    WHERE P.CategoryID = C.CategoryID
  ) AS MaximumPrice,
  (
    SELECT AVG(Price) 
    FROM Products P
    WHERE P.CategoryID = C.CategoryID
  ) AS AveragePrice
FROM Categories C;
CategoryIDCategoryNameMaximumPriceAveragePrice
1Beverages263.5037.979167
2Condiments43.9023.062500
3Confections81.0025.160000
SELECT
  ProductID, ProductName, CategoryID, Price
  -- ,(SELECT AVG(Price) FROM Products P2
  -- WHERE P2.CategoryID = P1.CategoryID)
FROM Products P1
WHERE Price < (
  SELECT AVG(Price) 
  FROM Products P2
  WHERE P2.CategoryID = P1.CategoryID
);
ProductIDProductNameCategoryIDPrice
1Chais118.00
2Chang119.00
3Aniseed Syrup210.00

EXISTS / NOT EXISTS 연산자

SELECT
  CategoryID, CategoryName
  -- ,(SELECT MAX(P.Price) FROM Products P
  -- WHERE P.CategoryID = C.CategoryID
  -- ) AS MaxPrice
FROM Categories C
WHERE EXISTS (
  SELECT * 
  FROM Products P
  WHERE P.CategoryID = C.CategoryID
  AND P.Price > 80
);
CategoryIDCategoryName
1Beverages
3Confections
6Meat/Poultry

0개의 댓글

관련 채용 정보