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 fcc3719

Browse files
Merge pull request iamAntimPal#88 from iamAntimPal/Branch-1
Branch 1
2 parents ab68a04 + 8617bc4 commit fcc3719

File tree

4 files changed

+631
-0
lines changed
  • LeetCode SQL 50 Solution

4 files changed

+631
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
# 🏆 Project Employees I - LeetCode 1075
3+
4+
## 📌 Problem Statement
5+
You are given two tables: **Project** and **Employee**.
6+
7+
### Project Table
8+
| Column Name | Type |
9+
| ----------- | ---- |
10+
| project_id | int |
11+
| employee_id | int |
12+
13+
- `(project_id, employee_id)` is the primary key of this table.
14+
- `employee_id` is a foreign key referencing the `Employee` table.
15+
16+
### Employee Table
17+
| Column Name | Type |
18+
| ---------------- | ------- |
19+
| employee_id | int |
20+
| name | varchar |
21+
| experience_years | int |
22+
23+
- `employee_id` is the primary key.
24+
- `experience_years` is guaranteed to be **NOT NULL**.
25+
26+
The task is to **return the average experience years of all employees for each project, rounded to 2 decimal places**.
27+
28+
---
29+
30+
## 📊 Example 1:
31+
### Input:
32+
**Project Table**
33+
| project_id | employee_id |
34+
| ---------- | ----------- |
35+
| 1 | 1 |
36+
| 1 | 2 |
37+
| 1 | 3 |
38+
| 2 | 1 |
39+
| 2 | 4 |
40+
41+
**Employee Table**
42+
| employee_id | name | experience_years |
43+
| ----------- | ------ | ---------------- |
44+
| 1 | Khaled | 3 |
45+
| 2 | Ali | 2 |
46+
| 3 | John | 1 |
47+
| 4 | Doe | 2 |
48+
49+
### Output:
50+
| project_id | average_years |
51+
| ---------- | ------------- |
52+
| 1 | 2.00 |
53+
| 2 | 2.50 |
54+
55+
### Explanation:
56+
- **Project 1:** `(3 + 2 + 1) / 3 = 2.00`
57+
- **Project 2:** `(3 + 2) / 2 = 2.50`
58+
59+
---
60+
61+
## 🖥 SQL Solutions
62+
63+
### 1️⃣ Standard MySQL Solution
64+
#### Explanation:
65+
- We **JOIN** the `Project` and `Employee` tables using `employee_id`.
66+
- We **calculate the average** of `experience_years` for each `project_id`.
67+
- We **round** the result to **two decimal places**.
68+
69+
```sql
70+
SELECT project_id, ROUND(AVG(experience_years), 2) AS average_years
71+
FROM project AS p
72+
LEFT JOIN employee AS e
73+
ON p.employee_id = e.employee_id
74+
GROUP BY project_id;
75+
```
76+
77+
---
78+
79+
### 2️⃣ Window Function (SQL) Solution
80+
#### Explanation:
81+
- Using **window functions**, we calculate the `AVG(experience_years)` over a **partitioned** dataset.
82+
83+
```sql
84+
SELECT DISTINCT project_id,
85+
ROUND(AVG(experience_years) OVER (PARTITION BY project_id), 2) AS average_years
86+
FROM project AS p
87+
JOIN employee AS e
88+
ON p.employee_id = e.employee_id;
89+
```
90+
91+
---
92+
93+
## 🐍 Pandas Solution (Python)
94+
#### Explanation:
95+
- We read both tables into Pandas **DataFrames**.
96+
- We merge the tables on `employee_id`.
97+
- We group by `project_id` and compute the mean.
98+
- We round the output to 2 decimal places.
99+
100+
```python
101+
import pandas as pd
102+
103+
def project_average_experience(project: pd.DataFrame, employee: pd.DataFrame) -> pd.DataFrame:
104+
df = project.merge(employee, on="employee_id")
105+
result = df.groupby("project_id")["experience_years"].mean().round(2).reset_index()
106+
result.columns = ["project_id", "average_years"]
107+
return result
108+
```
109+
110+
---
111+
112+
## 📁 File Structure
113+
```
114+
📂 Project-Employees-I
115+
│── 📜 README.md
116+
│── 📜 solution.sql
117+
│── 📜 solution_window.sql
118+
│── 📜 solution_pandas.py
119+
│── 📜 test_cases.sql
120+
```
121+
122+
---
123+
124+
## 🔗 Useful Links
125+
- 📖 [LeetCode Problem](https://leetcode.com/problems/project-employees-i/)
126+
- 📚 [SQL Joins Explanation](https://www.w3schools.com/sql/sql_join.asp)
127+
- 🐍 [Pandas Documentation](https://pandas.pydata.org/docs/)
128+
129+
---
130+
131+
## Let me know if you need any modifications! 🚀
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
# 📊 User Activity for the Past 30 Days I - LeetCode 1141
3+
4+
## 📌 Problem Statement
5+
You are given the **Activity** table that records user activities on a social media website.
6+
7+
### Activity Table
8+
| Column Name | Type |
9+
| ------------- | ---- |
10+
| user_id | int |
11+
| session_id | int |
12+
| activity_date | date |
13+
| activity_type | enum |
14+
15+
- The `activity_type` column is an ENUM of **('open_session', 'end_session', 'scroll_down', 'send_message')**.
16+
- Each session belongs to exactly **one user**.
17+
- The table **may have duplicate rows**.
18+
19+
### Task:
20+
Find the **daily active user count** for a period of **30 days ending 2019年07月27日 inclusively**.
21+
- A user is considered **active on a given day** if they made at least **one activity**.
22+
- Ignore days with **zero active users**.
23+
24+
---
25+
26+
## 📊 Example 1:
27+
### Input:
28+
**Activity Table**
29+
| user_id | session_id | activity_date | activity_type |
30+
| ------- | ---------- | ------------- | ------------- |
31+
| 1 | 1 | 2019年07月20日 | open_session |
32+
| 1 | 1 | 2019年07月20日 | scroll_down |
33+
| 1 | 1 | 2019年07月20日 | end_session |
34+
| 2 | 4 | 2019年07月20日 | open_session |
35+
| 2 | 4 | 2019年07月21日 | send_message |
36+
| 2 | 4 | 2019年07月21日 | end_session |
37+
| 3 | 2 | 2019年07月21日 | open_session |
38+
| 3 | 2 | 2019年07月21日 | send_message |
39+
| 3 | 2 | 2019年07月21日 | end_session |
40+
| 4 | 3 | 2019年06月25日 | open_session |
41+
| 4 | 3 | 2019年06月25日 | end_session |
42+
43+
### Output:
44+
| day | active_users |
45+
| ---------- | ------------ |
46+
| 2019年07月20日 | 2 |
47+
| 2019年07月21日 | 2 |
48+
49+
### Explanation:
50+
- **2019年07月20日**: Users **1 and 2** were active.
51+
- **2019年07月21日**: Users **2 and 3** were active.
52+
- **Days with zero active users are ignored**.
53+
54+
---
55+
56+
## 🖥 SQL Solutions
57+
58+
### 1️⃣ Standard MySQL Solution
59+
#### Explanation:
60+
- **Filter records** for the last **30 days** (ending on `2019年07月27日`).
61+
- Use `COUNT(DISTINCT user_id)` to count **unique active users per day**.
62+
- Ignore **days with zero active users**.
63+
64+
```sql
65+
SELECT
66+
activity_date AS day,
67+
COUNT(DISTINCT user_id) AS active_users
68+
FROM
69+
Activity
70+
WHERE
71+
DATEDIFF('2019年07月27日', activity_date) < 30
72+
AND DATEDIFF('2019年07月27日', activity_date) >= 0
73+
GROUP BY activity_date;
74+
```
75+
76+
---
77+
78+
### 2️⃣ Alternative Solution Using `BETWEEN`
79+
#### Explanation:
80+
- This solution filters the date range using `BETWEEN` instead of `DATEDIFF`.
81+
82+
```sql
83+
SELECT
84+
activity_date AS day,
85+
COUNT(DISTINCT user_id) AS active_users
86+
FROM
87+
Activity
88+
WHERE
89+
activity_date BETWEEN DATE_SUB('2019年07月27日', INTERVAL 29 DAY) AND '2019年07月27日'
90+
GROUP BY activity_date;
91+
```
92+
93+
---
94+
95+
## 🐍 Pandas Solution (Python)
96+
#### Explanation:
97+
- Filter activity records for the **last 30 days**.
98+
- **Group by `activity_date`** and count **unique `user_id`s**.
99+
- **Ignore days with zero active users**.
100+
101+
```python
102+
import pandas as pd
103+
104+
def daily_active_users(activity: pd.DataFrame) -> pd.DataFrame:
105+
# Filter data within the last 30 days (ending on '2019年07月27日')
106+
filtered = activity[(activity["activity_date"] >= "2019年06月28日") & (activity["activity_date"] <= "2019年07月27日")]
107+
108+
# Group by day and count unique users
109+
result = filtered.groupby("activity_date")["user_id"].nunique().reset_index()
110+
111+
# Rename columns
112+
result.columns = ["day", "active_users"]
113+
return result
114+
```
115+
116+
---
117+
118+
## 📁 File Structure
119+
```
120+
📂 User-Activity-Past-30-Days
121+
│── 📜 README.md
122+
│── 📜 solution.sql
123+
│── 📜 solution_between.sql
124+
│── 📜 solution_pandas.py
125+
│── 📜 test_cases.sql
126+
```
127+
128+
---
129+
130+
## 🔗 Useful Links
131+
- 📖 [LeetCode Problem](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/)
132+
- 📚 [SQL Date Functions](https://www.w3schools.com/sql/sql_dates.asp)
133+
- 🐍 [Pandas Documentation](https://pandas.pydata.org/docs/)
134+
135+
## Let me know if you need any changes! 🚀

0 commit comments

Comments
(0)

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