Skip to content

Commit 2037dbd

Browse files
committed
Update readme.md
1 parent 29c2563 commit 2037dbd

File tree

1 file changed

+170
-0
lines changed
  • LeetCode SQL 50 Solution/1327. List the Products Ordered in a Period

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
Here’s a well-structured `README.md` for **LeetCode 1327 - List the Products Ordered in a Period**, formatted for a GitHub repository:
2+
3+
```md
4+
# 🛒 List the Products Ordered in a Period - LeetCode 1327
5+
6+
## 📌 Problem Statement
7+
You are given two tables: **Products** and **Orders**.
8+
Your task is to **list the product names** that had at least **100 units ordered in February 2020** along with the total amount ordered.
9+
10+
---
11+
12+
## 📊 Table Structure
13+
14+
### **Products Table**
15+
| Column Name | Type |
16+
| ---------------- | ------- |
17+
| product_id | int |
18+
| product_name | varchar |
19+
| product_category | varchar |
20+
21+
- `product_id` is the **primary key** (unique identifier).
22+
- This table contains details about products.
23+
24+
---
25+
26+
### **Orders Table**
27+
| Column Name | Type |
28+
| ----------- | ---- |
29+
| product_id | int |
30+
| order_date | date |
31+
| unit | int |
32+
33+
- `product_id` is a **foreign key** referencing the `Products` table.
34+
- `order_date` represents when the order was placed.
35+
- `unit` represents the **number of products ordered** on that date.
36+
- The table **may contain duplicate rows**.
37+
38+
---
39+
40+
## 🔢 Goal:
41+
Find all products that had **at least 100 units ordered** during **February 2020** and display:
42+
- `product_name`
43+
- Total `unit` ordered in that period
44+
45+
---
46+
47+
## 📊 Example 1:
48+
### **Input:**
49+
#### **Products Table**
50+
| product_id | product_name | product_category |
51+
| ---------- | --------------------- | ---------------- |
52+
| 1 | Leetcode Solutions | Book |
53+
| 2 | Jewels of Stringology | Book |
54+
| 3 | HP | Laptop |
55+
| 4 | Lenovo | Laptop |
56+
| 5 | Leetcode Kit | T-shirt |
57+
58+
#### **Orders Table**
59+
| product_id | order_date | unit |
60+
| ---------- | ---------- | ---- |
61+
| 1 | 2020-02-05 | 60 |
62+
| 1 | 2020-02-10 | 70 |
63+
| 2 | 2020-01-18 | 30 |
64+
| 2 | 2020-02-11 | 80 |
65+
| 3 | 2020-02-17 | 2 |
66+
| 3 | 2020-02-24 | 3 |
67+
| 4 | 2020-03-01 | 20 |
68+
| 4 | 2020-03-04 | 30 |
69+
| 4 | 2020-03-04 | 60 |
70+
| 5 | 2020-02-25 | 50 |
71+
| 5 | 2020-02-27 | 50 |
72+
| 5 | 2020-03-01 | 50 |
73+
74+
### **Output:**
75+
| product_name | unit |
76+
| ------------------ | ---- |
77+
| Leetcode Solutions | 130 |
78+
| Leetcode Kit | 100 |
79+
80+
### **Explanation:**
81+
- **Leetcode Solutions** (ID=1) was ordered in February:
82+
\[
83+
60 + 70 = 130 \quad (\text{✓ included})
84+
\]
85+
- **Jewels of Stringology** (ID=2) was ordered **only 80** times in February. (**✗ not included**)
86+
- **HP Laptop** (ID=3) was ordered **5 times** in February. (**✗ not included**)
87+
- **Lenovo Laptop** (ID=4) was **not ordered** in February. (**✗ not included**)
88+
- **Leetcode Kit** (ID=5) was ordered **100 times** in February. (**✓ included**)
89+
90+
---
91+
92+
## 🖥 SQL Solution
93+
94+
### ✅ **Using `JOIN` + `GROUP BY` + `HAVING`**
95+
#### **Explanation:**
96+
1. **Join** the `Products` and `Orders` tables on `product_id`.
97+
2. **Filter orders** placed in **February 2020** (`BETWEEN '2020-02-01' AND '2020-02-29'`).
98+
3. **Sum up the `unit` ordered** for each product.
99+
4. **Use `HAVING` to filter products with at least 100 units.**
100+
5. Return results in **any order**.
101+
102+
```sql
103+
SELECT P.PRODUCT_NAME, SUM(O.UNIT) AS UNIT
104+
FROM PRODUCTS P
105+
INNER JOIN ORDERS O
106+
ON P.PRODUCT_ID = O.PRODUCT_ID
107+
WHERE O.ORDER_DATE BETWEEN '2020-02-01' AND '2020-02-29'
108+
GROUP BY P.PRODUCT_NAME
109+
HAVING SUM(O.UNIT) >= 100;
110+
```
111+
112+
---
113+
114+
## 🐍 Pandas Solution (Python)
115+
#### **Explanation:**
116+
1. **Merge** `products` and `orders` on `product_id`.
117+
2. **Filter only February 2020 orders**.
118+
3. **Group by `product_name`** and **sum `unit`**.
119+
4. **Filter products with at least 100 units**.
120+
5. **Return the final DataFrame**.
121+
122+
```python
123+
import pandas as pd
124+
125+
def products_ordered(products: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
126+
# Merge both tables on product_id
127+
merged_df = pd.merge(orders, products, on="product_id", how="inner")
128+
129+
# Convert order_date to datetime format and filter February 2020
130+
merged_df["order_date"] = pd.to_datetime(merged_df["order_date"])
131+
feb_orders = merged_df[
132+
(merged_df["order_date"] >= "2020-02-01") & (merged_df["order_date"] <= "2020-02-29")
133+
]
134+
135+
# Group by product_name and sum the units
136+
result = feb_orders.groupby("product_name")["unit"].sum().reset_index()
137+
138+
# Filter products with at least 100 units
139+
result = result[result["unit"] >= 100]
140+
141+
return result
142+
```
143+
144+
---
145+
146+
## 📁 File Structure
147+
```
148+
📂 List-Products-Ordered
149+
│── 📜 README.md
150+
│── 📜 solution.sql
151+
│── 📜 solution_pandas.py
152+
│── 📜 test_cases.sql
153+
```
154+
155+
---
156+
157+
## 🔗 Useful Links
158+
- 📖 [LeetCode Problem](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)
159+
- 📚 [SQL `HAVING` Clause](https://www.w3schools.com/sql/sql_having.asp)
160+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
161+
```
162+
163+
### Features of this `README.md`:
164+
✅ **Detailed problem statement with tables**
165+
✅ **Example with step-by-step calculations**
166+
✅ **SQL and Pandas solutions with explanations**
167+
✅ **File structure for easy organization**
168+
✅ **Helpful references for further reading**
169+
170+
Would you like any modifications? 🚀

0 commit comments

Comments
 (0)