1
+ -- Question 45
2
+ -- Table: Products
3
+
4
+ -- +------------------+---------+
5
+ -- | Column Name | Type |
6
+ -- +------------------+---------+
7
+ -- | product_id | int |
8
+ -- | product_name | varchar |
9
+ -- | product_category | varchar |
10
+ -- +------------------+---------+
11
+ -- product_id is the primary key for this table.
12
+ -- This table contains data about the company's products.
13
+ -- Table: Orders
14
+
15
+ -- +---------------+---------+
16
+ -- | Column Name | Type |
17
+ -- +---------------+---------+
18
+ -- | product_id | int |
19
+ -- | order_date | date |
20
+ -- | unit | int |
21
+ -- +---------------+---------+
22
+ -- There is no primary key for this table. It may have duplicate rows.
23
+ -- product_id is a foreign key to Products table.
24
+ -- unit is the number of products ordered in order_date.
25
+
26
+
27
+ -- Write an SQL query to get the names of products with greater than or equal to 100 units ordered in February 2020 and their amount.
28
+
29
+ -- Return result table in any order.
30
+
31
+ -- The query result format is in the following example:
32
+
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
+ -- Solution
79
+ Select a .product_name , a .unit
80
+ from
81
+ (select p .product_name , sum (unit) as unit
82
+ from orders o
83
+ join products p
84
+ on o .product_id = p .product_id
85
+ where month(order_date)= 2 and year(order_date) = 2020
86
+ group by o .product_id ) a
87
+ where a .unit >= 100
0 commit comments