Find products with total ordered units ≥ 100 in February 2020 using JOIN and HAVING
IntuitionThe problem requires finding products that were ordered in February 2020 with a total order quantity of at least 100 units. This can be efficiently sol
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires finding products that were ordered in February 2020 with a total order quantity of at least 100 units. This can be efficiently solved using SQL aggregation and filtering.


# Approach
<!-- Describe your approach to solving the problem. -->
1. **Join** the PRODUCTS and ORDERS tables using product_id.
2. **Filte**r the orders within the **date range** '2020-02-01' to '2020-02-29'.
3. **Aggregate the total ordered** units per product using SUM(O.unit).
4. Use HAVING to **filter products** with at least 100 ordered units.
5. Group by P.product_id to **get results per product**.


# Complexity
- **Time complexity: O(n)**
The query processes all rows in the ORDERS table and aggregates based on product_id.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->


- **Space complexity: O(1)**
SQL operations are performed in-place without extra memory usage.

<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```mysql []
SELECT P.product_name,
SUM(O.unit) AS unit 
FROM PRODUCTS P JOIN ORDERS O 
ON P.product_id = O.product_id
WHERE O.order_date 
BETWEEN '2020-02-01' AND '2020-02-29'
GROUP BY P.product_id
HAVING SUM(O.unit) >= 100;
```
['MySQL']