Products 테이블:
product_id, product_name, product_categoryproduct_id는 Primary Key로, 회사의 제품 정보를 담고 있음.Orders 테이블:
product_id, order_date, unitproduct_id는 Products 테이블의 Foreign Key이며, unit은 해당 날짜에 주문된 제품의 수량을 나타냄.Products 테이블:
+-------------+-----------------------+------------------+
| product_id | product_name | product_category |
+-------------+-----------------------+------------------+
| 1 | Leetcode Solutions | Book |
| 2 | Jewels of Stringology | Book |
| 3 | HP | Laptop |
| 4 | Lenovo | Laptop |
| 5 | Leetcode Kit | T-shirt |
+-------------+-----------------------+------------------+
Orders 테이블:
+--------------+--------------+----------+
| product_id | order_date | unit |
+--------------+--------------+----------+
| 1 | 2020-02-05 | 60 |
| 1 | 2020-02-10 | 70 |
| 2 | 2020-01-18 | 30 |
| 2 | 2020-02-11 | 80 |
| 3 | 2020-02-17 | 2 |
| 3 | 2020-02-24 | 3 |
| 4 | 2020-03-01 | 20 |
| 4 | 2020-03-04 | 30 |
| 4 | 2020-03-04 | 60 |
| 5 | 2020-02-25 | 50 |
| 5 | 2020-02-27 | 50 |
| 5 | 2020-03-01 | 50 |
+--------------+--------------+----------+
출력 예시:
+--------------------+---------+
| product_name | unit |
+--------------------+---------+
| Leetcode Solutions | 130 |
| Leetcode Kit | 100 |
+--------------------+---------+
Orders 테이블에서 product_id별로 그룹화하여, 2020년 2월의 주문 수량을 합산.HAVING 조건을 통해 100개 이상의 주문 수량을 가진 제품을 필터링.Products 테이블과 연결하여 제품 이름과 주문 수량을 가져옴.select product_name, unit
from Products,
(select product_id, sum(unit) unit
from Orders
where month(order_date) = 2 and year(order_date) = 2020
group by product_id
having unit >= 100) as id
where Products.product_id = id.product_id
Orders 테이블에서 2020년 2월에 해당하는 데이터만 필터링하고, product_id별로 주문 수량을 합산하여 100개 이상인 제품만 선택.Products 테이블과 서브쿼리를 쉼표로 나열하여 결합하고, WHERE 절에서 product_id가 일치하는 행을 필터링합니다.WHERE 절에서 MONTH()와 YEAR() 함수를 사용하여 특정 월과 연도를 필터링하는 방법을 학습.HAVING 절을 사용하여 그룹화된 결과에 조건을 적용하는 방법을 익힘.JOIN 없이 서브쿼리를 통해 두 테이블을 결합하는 다양한 접근 방식의 중요성을 이해.