You have two tables: Products and Orders.
The Products table contains information about the company's products with columns:
product_id(primary key): unique identifier for each productproduct_name: name of the productproduct_category: category of the product
The Orders table contains order information with columns:
product_id(foreign key): references Products tableorder_date: date when the order was placedunit: number of units ordered
Write a SQL query to find the names of products that have at least 100 units ordered in February 2020 and return their total amount ordered.
Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Primary key, unique identifier for each product |
product_name
|
varchar | Name of the product |
product_category
|
varchar | Category of the product |
| Column Name | Type | Description |
|---|---|---|
product_id
|
int | Foreign key referencing Products table |
order_date
|
date | Date when the order was placed |
unit
|
int | Number of units ordered |
Input & Output
| product_id | product_name | product_category |
|---|---|---|
| 1 | Leetcode Solutions | Book |
| 2 | Jewels of Stringology | Book |
| 3 | HP | Laptop |
| 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 |
| product_name | unit |
|---|---|
| Leetcode Solutions | 130 |
Leetcode Solutions (product_id=1) has orders of 60 units on 2020-02-05 and 70 units on 2020-02-10, totaling 130 units in February 2020, which exceeds 100. Other products either don't reach 100 units in February 2020 or don't exist in the Products table.
| product_id | product_name | product_category |
|---|---|---|
| 1 | Mouse | Electronics |
| 2 | Keyboard | Electronics |
| product_id | order_date | unit |
|---|---|---|
| 1 | 2020-02-05 | 30 |
| 1 | 2020-02-10 | 40 |
| 2 | 2020-02-15 | 50 |
| product_name | unit |
|---|
No products have at least 100 units ordered in February 2020. Mouse has 70 total units (30+40) and Keyboard has 50 units, both below the 100-unit threshold.
Constraints
-
1 ≤ product_id ≤ 1000 -
product_nameandproduct_categoryare non-empty strings -
order_dateis a valid date -
unit ≥ 1