Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd802c7

Browse files
Update readme.md
1 parent f863d4f commit cd802c7

File tree

1 file changed

+140
-0
lines changed
  • LeetCode SQL 50 Solution/1075. Project Employees I

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
Here's a well-formatted `README.md` file for your GitHub repository with the SQL solutions:
2+
3+
```md
4+
# 🏆 Project Employees I - LeetCode 1075
5+
6+
## 📌 Problem Statement
7+
You are given two tables: **Project** and **Employee**.
8+
9+
### Project Table
10+
| Column Name | Type |
11+
| ----------- | ---- |
12+
| project_id | int |
13+
| employee_id | int |
14+
15+
- `(project_id, employee_id)` is the primary key of this table.
16+
- `employee_id` is a foreign key referencing the `Employee` table.
17+
18+
### Employee Table
19+
| Column Name | Type |
20+
| ---------------- | ------- |
21+
| employee_id | int |
22+
| name | varchar |
23+
| experience_years | int |
24+
25+
- `employee_id` is the primary key.
26+
- `experience_years` is guaranteed to be **NOT NULL**.
27+
28+
The task is to **return the average experience years of all employees for each project, rounded to 2 decimal places**.
29+
30+
---
31+
32+
## 📊 Example 1:
33+
### Input:
34+
**Project Table**
35+
| project_id | employee_id |
36+
| ---------- | ----------- |
37+
| 1 | 1 |
38+
| 1 | 2 |
39+
| 1 | 3 |
40+
| 2 | 1 |
41+
| 2 | 4 |
42+
43+
**Employee Table**
44+
| employee_id | name | experience_years |
45+
| ----------- | ------ | ---------------- |
46+
| 1 | Khaled | 3 |
47+
| 2 | Ali | 2 |
48+
| 3 | John | 1 |
49+
| 4 | Doe | 2 |
50+
51+
### Output:
52+
| project_id | average_years |
53+
| ---------- | ------------- |
54+
| 1 | 2.00 |
55+
| 2 | 2.50 |
56+
57+
### Explanation:
58+
- **Project 1:** `(3 + 2 + 1) / 3 = 2.00`
59+
- **Project 2:** `(3 + 2) / 2 = 2.50`
60+
61+
---
62+
63+
## 🖥 SQL Solutions
64+
65+
### 1️⃣ Standard MySQL Solution
66+
#### Explanation:
67+
- We **JOIN** the `Project` and `Employee` tables using `employee_id`.
68+
- We **calculate the average** of `experience_years` for each `project_id`.
69+
- We **round** the result to **two decimal places**.
70+
71+
```sql
72+
SELECT project_id, ROUND(AVG(experience_years), 2) AS average_years
73+
FROM project AS p
74+
LEFT JOIN employee AS e
75+
ON p.employee_id = e.employee_id
76+
GROUP BY project_id;
77+
```
78+
79+
---
80+
81+
### 2️⃣ Window Function (SQL) Solution
82+
#### Explanation:
83+
- Using **window functions**, we calculate the `AVG(experience_years)` over a **partitioned** dataset.
84+
85+
```sql
86+
SELECT DISTINCT project_id,
87+
ROUND(AVG(experience_years) OVER (PARTITION BY project_id), 2) AS average_years
88+
FROM project AS p
89+
JOIN employee AS e
90+
ON p.employee_id = e.employee_id;
91+
```
92+
93+
---
94+
95+
## 🐍 Pandas Solution (Python)
96+
#### Explanation:
97+
- We read both tables into Pandas **DataFrames**.
98+
- We merge the tables on `employee_id`.
99+
- We group by `project_id` and compute the mean.
100+
- We round the output to 2 decimal places.
101+
102+
```python
103+
import pandas as pd
104+
105+
def project_average_experience(project: pd.DataFrame, employee: pd.DataFrame) -> pd.DataFrame:
106+
df = project.merge(employee, on="employee_id")
107+
result = df.groupby("project_id")["experience_years"].mean().round(2).reset_index()
108+
result.columns = ["project_id", "average_years"]
109+
return result
110+
```
111+
112+
---
113+
114+
## 📁 File Structure
115+
```
116+
📂 Project-Employees-I
117+
│── 📜 README.md
118+
│── 📜 solution.sql
119+
│── 📜 solution_window.sql
120+
│── 📜 solution_pandas.py
121+
│── 📜 test_cases.sql
122+
```
123+
124+
---
125+
126+
## 🔗 Useful Links
127+
- 📖 [LeetCode Problem](https://leetcode.com/problems/project-employees-i/)
128+
- 📚 [SQL Joins Explanation](https://www.w3schools.com/sql/sql_join.asp)
129+
- 🐍 [Pandas Documentation](https://pandas.pydata.org/docs/)
130+
```
131+
132+
This README provides:
133+
- A **clear problem statement**
134+
- **Input and output tables**
135+
- **Multiple SQL solutions with explanations**
136+
- **A Python Pandas solution**
137+
- **File structure**
138+
- **Useful links**
139+
140+
Let me know if you need any modifications! 🚀

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /