Skip to content

Commit 3b9bdc3

Browse files
committed
Update readme.md
1 parent 7895ac3 commit 3b9bdc3

File tree

1 file changed

+154
-0
lines changed
  • LeetCode SQL 50 Solution/1378. Replace Employee ID With The Unique Identifier

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
Here’s a well-structured `README.md` for **LeetCode 1378 - Replace Employee ID With The Unique Identifier**, formatted for a GitHub repository:
2+
3+
```md
4+
# 🏢 Replace Employee ID With The Unique Identifier - LeetCode 1378
5+
6+
## 📌 Problem Statement
7+
You are given two tables: **Employees** and **EmployeeUNI**.
8+
9+
Your task is to return a table with:
10+
- Each employee's **unique ID** if it exists.
11+
- If an employee **does not** have a unique ID, return `NULL`.
12+
13+
The result can be returned in **any order**.
14+
15+
---
16+
17+
## 📊 Table Structure
18+
19+
### **Employees Table**
20+
| Column Name | Type |
21+
| ----------- | ------- |
22+
| id | int |
23+
| name | varchar |
24+
25+
- `id` is the **primary key** (unique for each employee).
26+
- `name` is the **employee's name**.
27+
28+
---
29+
30+
### **EmployeeUNI Table**
31+
| Column Name | Type |
32+
| ----------- | ---- |
33+
| id | int |
34+
| unique_id | int |
35+
36+
- `(id, unique_id)` is the **primary key** (ensuring unique mapping of employee IDs to unique IDs).
37+
- Each employee **may or may not** have a corresponding **unique ID**.
38+
39+
---
40+
41+
## 📊 Example 1:
42+
### **Input:**
43+
#### **Employees Table**
44+
| id | name |
45+
| --- | -------- |
46+
| 1 | Alice |
47+
| 7 | Bob |
48+
| 11 | Meir |
49+
| 90 | Winston |
50+
| 3 | Jonathan |
51+
52+
#### **EmployeeUNI Table**
53+
| id | unique_id |
54+
| --- | --------- |
55+
| 3 | 1 |
56+
| 11 | 2 |
57+
| 90 | 3 |
58+
59+
### **Output:**
60+
| unique_id | name |
61+
| --------- | -------- |
62+
| null | Alice |
63+
| null | Bob |
64+
| 2 | Meir |
65+
| 3 | Winston |
66+
| 1 | Jonathan |
67+
68+
### **Explanation:**
69+
- `Alice` and `Bob` **do not have** a unique ID, so we return `NULL`.
70+
- The **unique ID** of `Meir` is **2**.
71+
- The **unique ID** of `Winston` is **3**.
72+
- The **unique ID** of `Jonathan` is **1**.
73+
74+
---
75+
76+
## 🖥 SQL Solution
77+
78+
### ✅ **Using `LEFT JOIN`**
79+
#### **Explanation:**
80+
- Use a **LEFT JOIN** to **include all employees**.
81+
- If an employee **does not have** a matching `unique_id`, return `NULL`.
82+
83+
```sql
84+
SELECT eu.unique_id, e.name
85+
FROM Employees e
86+
LEFT JOIN EmployeeUNI eu
87+
ON e.id = eu.id;
88+
```
89+
90+
### **Using `USING(id)`**
91+
#### **Explanation:**
92+
- `USING(id)` is a cleaner alternative when both tables share a column.
93+
94+
```sql
95+
SELECT unique_id, name
96+
FROM Employees
97+
LEFT JOIN EmployeeUNI
98+
USING (id);
99+
```
100+
101+
### **Sorting by `id` (Optional)**
102+
#### **Explanation:**
103+
- If you want to return the result **sorted by `id`**, add `ORDER BY e.id`:
104+
105+
```sql
106+
SELECT eu.unique_id, e.name
107+
FROM Employees e
108+
LEFT JOIN EmployeeUNI eu
109+
ON e.id = eu.id
110+
ORDER BY e.id;
111+
```
112+
113+
---
114+
115+
## 🐍 Pandas Solution (Python)
116+
#### **Explanation:**
117+
- Merge `Employees` with `EmployeeUNI` **using `left` join** on `id`.
118+
- Fill missing values (`NaN`) with `None`.
119+
120+
```python
121+
import pandas as pd
122+
123+
def replace_employee_id(employees: pd.DataFrame, employee_uni: pd.DataFrame) -> pd.DataFrame:
124+
merged_df = employees.merge(employee_uni, on="id", how="left")
125+
return merged_df[["unique_id", "name"]]
126+
```
127+
128+
---
129+
130+
## 📁 File Structure
131+
```
132+
📂 Replace-Employee-ID
133+
│── 📜 README.md
134+
│── 📜 solution.sql
135+
│── 📜 solution_pandas.py
136+
│── 📜 test_cases.sql
137+
```
138+
139+
---
140+
141+
## 🔗 Useful Links
142+
- 📖 [LeetCode Problem](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/)
143+
- 📚 [SQL `LEFT JOIN`](https://www.w3schools.com/sql/sql_join_left.asp)
144+
- 🐍 [Pandas Merge Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html)
145+
```
146+
147+
### Features of this `README.md`:
148+
✅ **Clear problem description with tables**
149+
✅ **Example with step-by-step explanation**
150+
✅ **SQL and Pandas solutions with detailed breakdowns**
151+
✅ **File structure for easy organization**
152+
✅ **Helpful references for further learning**
153+
154+
Would you like any modifications? 🚀

0 commit comments

Comments
 (0)