1327. List the Products Ordered in a Period
Problem Statement
Get the names of products with total order units of 100 or more in February 2020. Return the result table in any order.
Table Schema
Table: Products
+------------------+---------+
| product_id | int |
| product_name | varchar |
| product_category | varchar |
+------------------+---------+
product_id is the primary key.
Table: Orders
+-------------+------+
| product_id | int |
| order_date | date |
| unit | int |
+-------------+------+
No primary key; may have duplicates.
Examples
Example 1
Input Table:
Products table:
+------------+--------------------+------------------+
| product_id | product_name | product_category |
+------------+--------------------+------------------+
| 1 | Leetcode Solutions | Book |
| 2 | Jewels of Stringology | Book |
+------------+--------------------+------------------+
Orders table:
+------------+--------------+------+
| product_id | order_date | unit |
+------------+--------------+------+
| 1 | 2020-02-05 | 60 |
| 1 | 2020-02-10 | 70 |
| 2 | 2020-01-18 | 30 |
+------------+--------------+------+
Expected Output:
+--------------------+------+
| product_name | unit |
+--------------------+------+
| Leetcode Solutions | 130 |
+--------------------+------+
SQL Solution
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, p.product_name
HAVING SUM(o.unit) >= 100;
Problem Info
DifficultyEASY
Topics
joingroup-byhaving