Skip to content

Commit 93d3f17

Browse files
committed
Update readme.md
1 parent 7009703 commit 93d3f17

File tree

1 file changed

+168
-0
lines changed
  • LeetCode SQL 50 Solution/1068. Product Sales Analysis I

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
2+
3+
# **1068. Product Sales Analysis I**
4+
5+
## **Problem Statement**
6+
You are given two tables:
7+
8+
- `Sales` (contains sales data including `product_id`, `year`, `quantity`, and `price`).
9+
- `Product` (contains `product_id` and `product_name`).
10+
11+
Each `product_id` in `Sales` is a **foreign key** referring to the `Product` table.
12+
13+
### **Sales Table**
14+
```
15+
+---------+------------+------+----------+-------+
16+
| sale_id | product_id | year | quantity | price |
17+
+---------+------------+------+----------+-------+
18+
| int | int | int | int | int |
19+
+---------+------------+------+----------+-------+
20+
```
21+
- `(sale_id, year)` is the **primary key** (unique values).
22+
- `product_id` refers to the `Product` table.
23+
- `price` represents the **per unit price** of the product in that year.
24+
25+
### **Product Table**
26+
```
27+
+------------+--------------+
28+
| product_id | product_name |
29+
+------------+--------------+
30+
| int | varchar |
31+
+------------+--------------+
32+
```
33+
- `product_id` is the **primary key** of this table.
34+
35+
### **Task:**
36+
Find the `product_name`, `year`, and `price` for each sale in the `Sales` table.
37+
38+
---
39+
40+
## **Example 1:**
41+
42+
### **Input:**
43+
**Sales Table**
44+
```
45+
+---------+------------+------+----------+-------+
46+
| sale_id | product_id | year | quantity | price |
47+
+---------+------------+------+----------+-------+
48+
| 1 | 100 | 2008 | 10 | 5000 |
49+
| 2 | 100 | 2009 | 12 | 5000 |
50+
| 7 | 200 | 2011 | 15 | 9000 |
51+
+---------+------------+------+----------+-------+
52+
```
53+
54+
**Product Table**
55+
```
56+
+------------+--------------+
57+
| product_id | product_name |
58+
+------------+--------------+
59+
| 100 | Nokia |
60+
| 200 | Apple |
61+
| 300 | Samsung |
62+
+------------+--------------+
63+
```
64+
65+
### **Output:**
66+
```
67+
+--------------+-------+-------+
68+
| product_name | year | price |
69+
+--------------+-------+-------+
70+
| Nokia | 2008 | 5000 |
71+
| Nokia | 2009 | 5000 |
72+
| Apple | 2011 | 9000 |
73+
+--------------+-------+-------+
74+
```
75+
76+
### **Explanation:**
77+
- **Sale ID 1:** `Nokia` was sold in **2008** for **5000**.
78+
- **Sale ID 2:** `Nokia` was sold in **2009** for **5000**.
79+
- **Sale ID 7:** `Apple` was sold in **2011** for **9000**.
80+
81+
---
82+
83+
## **SQL Solutions**
84+
85+
### **1️⃣ Standard MySQL Solution**
86+
```sql
87+
SELECT p.product_name, s.year, s.price
88+
FROM Sales s
89+
JOIN Product p ON s.product_id = p.product_id;
90+
```
91+
#### **Explanation:**
92+
1. **JOIN** the `Sales` table with the `Product` table using `product_id`.
93+
2. **Select `product_name`, `year`, and `price`** from the joined result.
94+
95+
---
96+
97+
### **2️⃣ Window Function (SQL) Solution**
98+
```sql
99+
WITH SalesData AS (
100+
SELECT s.product_id, s.year, s.price, p.product_name
101+
FROM Sales s
102+
JOIN Product p ON s.product_id = p.product_id
103+
)
104+
SELECT product_name, year, price
105+
FROM SalesData;
106+
```
107+
#### **Explanation:**
108+
1. **CTE `SalesData`** → Stores the joined data from `Sales` and `Product`.
109+
2. **Final SELECT** → Retrieves `product_name`, `year`, and `price`.
110+
111+
---
112+
113+
## **Pandas Solution (Python)**
114+
```python
115+
import pandas as pd
116+
117+
# Sample Data
118+
sales_data = {'sale_id': [1, 2, 7],
119+
'product_id': [100, 100, 200],
120+
'year': [2008, 2009, 2011],
121+
'quantity': [10, 12, 15],
122+
'price': [5000, 5000, 9000]}
123+
124+
product_data = {'product_id': [100, 200, 300],
125+
'product_name': ['Nokia', 'Apple', 'Samsung']}
126+
127+
# Create DataFrames
128+
sales_df = pd.DataFrame(sales_data)
129+
product_df = pd.DataFrame(product_data)
130+
131+
# Perform Join
132+
result = sales_df.merge(product_df, on='product_id')[['product_name', 'year', 'price']]
133+
134+
print(result)
135+
```
136+
137+
### **Explanation:**
138+
1. **Create DataFrames** → Convert `Sales` and `Product` tables into Pandas DataFrames.
139+
2. **Perform `merge()` on `product_id`** → Equivalent to SQL `JOIN`.
140+
3. **Select required columns (`product_name`, `year`, `price`)**.
141+
142+
---
143+
144+
## **File Structure**
145+
```
146+
LeetCode1068/
147+
├── problem_statement.md # Contains the problem description and constraints.
148+
├── sql_solution.sql # Contains the SQL solutions (Standard + Window Functions).
149+
├── pandas_solution.py # Contains the Pandas solution.
150+
├── README.md # Overview of the problem and available solutions.
151+
```
152+
153+
---
154+
155+
## **Useful Links**
156+
- [LeetCode Problem 1068](https://leetcode.com/problems/product-sales-analysis-i/)
157+
- [SQL JOIN Documentation](https://www.w3schools.com/sql/sql_join.asp)
158+
- [Pandas Merge Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)
159+
160+
---
161+
162+
This README now includes:
163+
**MySQL Query**
164+
**Window SQL Query**
165+
**Pandas Python Solution**
166+
**File Structure & Useful Links**
167+
168+
🚀 **Now it's a complete guide!** 🚀

0 commit comments

Comments
 (0)