1+ 1327 . List the Products Ordered in a Period
2+ Solved
3+ Easy
4+ Topics
5+ Companies
6+ SQL Schema
7+ Pandas Schema
8+ Table: Products
9+
10+ + -- ----------------+---------+
11+ | Column Name | Type |
12+ + -- ----------------+---------+
13+ | product_id | int |
14+ | product_name | varchar |
15+ | product_category | varchar |
16+ + -- ----------------+---------+
17+ product_id is the primary key (column with unique values ) for this table.
18+ This table contains data about the company' s products.
19+
20+
21+ Table: Orders
22+
23+ +---------------+---------+
24+ | Column Name | Type |
25+ +---------------+---------+
26+ | product_id | int |
27+ | order_date | date |
28+ | unit | int |
29+ +---------------+---------+
30+ This table may have duplicate rows.
31+ product_id is a foreign key (reference column) to the Products table.
32+ unit is the number of products ordered in order_date.
33+
34+
35+ Write a solution to get the names of products that have at least 100 units ordered in February 2020 and their amount.
36+
37+ Return the result table in any order.
38+
39+ The result format is in the following example.
40+
41+
42+
43+ Example 1:
44+
45+ Input:
46+ Products table:
47+ +-------------+-----------------------+------------------+
48+ | product_id | product_name | product_category |
49+ +-------------+-----------------------+------------------+
50+ | 1 | Leetcode Solutions | Book |
51+ | 2 | Jewels of Stringology | Book |
52+ | 3 | HP | Laptop |
53+ | 4 | Lenovo | Laptop |
54+ | 5 | Leetcode Kit | T-shirt |
55+ +-------------+-----------------------+------------------+
56+ Orders table:
57+ +--------------+--------------+----------+
58+ | product_id | order_date | unit |
59+ +--------------+--------------+----------+
60+ | 1 | 2020-02-05 | 60 |
61+ | 1 | 2020-02-10 | 70 |
62+ | 2 | 2020-01-18 | 30 |
63+ | 2 | 2020-02-11 | 80 |
64+ | 3 | 2020-02-17 | 2 |
65+ | 3 | 2020-02-24 | 3 |
66+ | 4 | 2020-03-01 | 20 |
67+ | 4 | 2020-03-04 | 30 |
68+ | 4 | 2020-03-04 | 60 |
69+ | 5 | 2020-02-25 | 50 |
70+ | 5 | 2020-02-27 | 50 |
71+ | 5 | 2020-03-01 | 50 |
72+ +--------------+--------------+----------+
73+ Output:
74+ +--------------------+---------+
75+ | product_name | unit |
76+ +--------------------+---------+
77+ | Leetcode Solutions | 130 |
78+ | Leetcode Kit | 100 |
79+ +--------------------+---------+
80+ Explanation:
81+ Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
82+ Products with product_id = 2 is ordered in February a total of 80.
83+ Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
84+ Products with product_id = 4 was not ordered in February 2020.
85+ Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.
86+ ' ' '
87+
88+ # Write your MySQL query statement below
89+ SELECT P .PRODUCT_NAME , SUM (O .UNIT ) AS UNIT
90+ FROM PRODUCTS P
91+ INNER JOIN ORDERS O
92+ ON P .PRODUCT_ID = O .PRODUCT_ID
93+ WHERE O .ORDER_DATE BETWEEN ' 2020-02-01' AND ' 2020-02-29'
94+ GROUP BY P .PRODUCT_NAME
95+ HAVING SUM (O .UNIT ) >= 100 ;
0 commit comments