Replace Employee ID With The Unique Identifier - Problem
You're working at a company that's migrating from an old employee ID system to a new unique identifier system. Your task is to map employee information from the old system to the new one.
You have two tables:
- Employees: Contains employee
idandname - EmployeeUNI: Contains the mapping between old
idand newunique_id
Your goal is to create a report that shows each employee's unique_id and name. If an employee doesn't have a unique_id in the new system yet, show null for their unique_id.
This is a classic LEFT JOIN scenario where we want all employees, whether they have unique IDs or not.
Input & Output
example_1.sql โ Basic Example
$
Input:
Employees table:
| id | name |
|----|-------|
| 1 | Alice |
| 7 | Bob |
| 11 | Meir |
EmployeeUNI table:
| id | unique_id |
|----|----------|
| 1 | 101 |
| 7 | 102 |
โบ
Output:
| unique_id | name |
|-----------|-------|
| 101 | Alice |
| 102 | Bob |
| null | Meir |
๐ก Note:
Alice (ID=1) maps to unique_id 101, Bob (ID=7) maps to unique_id 102, and Meir (ID=11) has no mapping so gets null
example_2.sql โ All Employees Have Unique IDs
$
Input:
Employees table:
| id | name |
|----|------|
| 1 | John |
| 2 | Jane |
EmployeeUNI table:
| id | unique_id |
|----|----------|
| 1 | 201 |
| 2 | 202 |
โบ
Output:
| unique_id | name |
|-----------|------|
| 201 | John |
| 202 | Jane |
๐ก Note:
All employees have corresponding unique IDs, so no null values in result
example_3.sql โ No Employees Have Unique IDs
$
Input:
Employees table:
| id | name |
|----|-------|
| 1 | Alice |
| 2 | Bob |
EmployeeUNI table:
| id | unique_id |
|----|----------|
(empty)
โบ
Output:
| unique_id | name |
|-----------|-------|
| null | Alice |
| null | Bob |
๐ก Note:
No employees have unique IDs yet, so all get null values while preserving their names
Visualization
Tap to expand
Understanding the Visualization
1
All Members Preserved
LEFT JOIN ensures every employee appears in results
2
Match Where Possible
When unique_id exists, include it in the result
3
Fill Gaps with NULL
When no unique_id exists, use null placeholder
Key Takeaway
๐ฏ Key Insight: LEFT JOIN is perfect when you need ALL records from one table, with optional matching data from another table
Time & Space Complexity
Time Complexity
O(n)
Single pass through both tables with hash join optimization
โ Linear Growth
Space Complexity
O(n)
Space for hash table and result set
โก Linearithmic Space
Constraints
- 1 โค employees.length โค 1000
- 1 โค employeeUNI.length โค 1000
- 1 โค id โค 106
- Each row in EmployeeUNI has unique (id, unique_id) pairs
- Employee names contain only English letters and spaces
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code