|
| 1 | +/* |
| 2 | +List the Products Ordered in a Period |
| 3 | +
|
| 4 | +Table: Products |
| 5 | ++------------------+---------+ |
| 6 | +| Column Name | Type | |
| 7 | ++------------------+---------+ |
| 8 | +| product_id | int | |
| 9 | +| product_name | varchar | |
| 10 | +| product_category | varchar | |
| 11 | ++------------------+---------+ |
| 12 | +product_id is the primary key for this table. |
| 13 | +This table contains data about the company's products. |
| 14 | +
|
| 15 | +Table: Orders |
| 16 | ++---------------+---------+ |
| 17 | +| Column Name | Type | |
| 18 | ++---------------+---------+ |
| 19 | +| product_id | int | |
| 20 | +| order_date | date | |
| 21 | +| unit | int | |
| 22 | ++---------------+---------+ |
| 23 | +There is no primary key for this table. It may have duplicate rows. |
| 24 | +product_id is a foreign key to Products table. |
| 25 | +unit is the number of products ordered in order_date. |
| 26 | + |
| 27 | +Write an SQL query to get the names of products with greater than or equal to 100 units ordered in February 2020 |
| 28 | +and their amount. |
| 29 | +
|
| 30 | +Return result table in any order. |
| 31 | +
|
| 32 | +The query result format is in the following example: |
| 33 | +
|
| 34 | +
|
| 35 | +Products table: |
| 36 | ++-------------+-----------------------+------------------+ |
| 37 | +| product_id | product_name | product_category | |
| 38 | ++-------------+-----------------------+------------------+ |
| 39 | +| 1 | Leetcode Solutions | Book | |
| 40 | +| 2 | Jewels of Stringology | Book | |
| 41 | +| 3 | HP | Laptop | |
| 42 | +| 4 | Lenovo | Laptop | |
| 43 | +| 5 | Leetcode Kit | T-shirt | |
| 44 | ++-------------+-----------------------+------------------+ |
| 45 | +
|
| 46 | +Orders table: |
| 47 | ++--------------+--------------+----------+ |
| 48 | +| product_id | order_date | unit | |
| 49 | ++--------------+--------------+----------+ |
| 50 | +| 1 | 2020-02-05 | 60 | |
| 51 | +| 1 | 2020-02-10 | 70 | |
| 52 | +| 2 | 2020-01-18 | 30 | |
| 53 | +| 2 | 2020-02-11 | 80 | |
| 54 | +| 3 | 2020-02-17 | 2 | |
| 55 | +| 3 | 2020-02-24 | 3 | |
| 56 | +| 4 | 2020-03-01 | 20 | |
| 57 | +| 4 | 2020-03-04 | 30 | |
| 58 | +| 4 | 2020-03-04 | 60 | |
| 59 | +| 5 | 2020-02-25 | 50 | |
| 60 | +| 5 | 2020-02-27 | 50 | |
| 61 | +| 5 | 2020-03-01 | 50 | |
| 62 | ++--------------+--------------+----------+ |
| 63 | +
|
| 64 | +Result table: |
| 65 | ++--------------------+---------+ |
| 66 | +| product_name | unit | |
| 67 | ++--------------------+---------+ |
| 68 | +| Leetcode Solutions | 130 | |
| 69 | +| Leetcode Kit | 100 | |
| 70 | ++--------------------+---------+ |
| 71 | +
|
| 72 | +Products with product_id = 1 is ordered in February a total of (60 + 70) = 130. |
| 73 | +Products with product_id = 2 is ordered in February a total of 80. |
| 74 | +Products with product_id = 3 is ordered in February a total of (2 + 3) = 5. |
| 75 | +Products with product_id = 4 was not ordered in February 2020. |
| 76 | +Products with product_id = 5 is ordered in February a total of (50 + 50) = 100. |
| 77 | +*/ |
| 78 | + |
| 79 | +# MY SOLUTION |
| 80 | +SELECT p.product_name, SUM(o.unit) AS unit |
| 81 | +FROM Products p |
| 82 | +LEFT JOIN Orders o |
| 83 | +ON p.product_id = o.product_id |
| 84 | +WHERE o.order_date BETWEEN '2020-02-01' AND '2020-02-29' |
| 85 | +GROUP BY p.product_id |
| 86 | +HAVING unit >= 100 |
0 commit comments