1378. Replace Employee ID With The Unique Identifier - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

You have two tables, Employees and EmployeeUNI, in a company database.

  • Employees
    • id (int, primary key)
    • name (varchar)
  • EmployeeUNI
    • id (int)
    • unique_id (int)

Each row in EmployeeUNI maps an employee’s id to a company‑wide unique_id. Not every employee appears in EmployeeUNI. Write a query that returns each employee’s name alongside their unique_id, or NULL when no mapping exists. The order of rows does not matter.

Examples

Example
Employees

idname
1Alice
7Bob
11Meir
90Winston
3Jonathan

EmployeeUNI

idunique_id
31
112
903

Result

unique_idname
NULLAlice
NULLBob
2Meir
3Winston
1Jonathan

Constraints

  • Total rows in Employees and EmployeeUNI each ≤ 10⁵
  • Employee id values are unique in Employees
  • (id, unique_id) pairs are unique in EmployeeUNI

Approach

LEFT JOIN

  1. Start from Employees so every employee appears in the result.
  2. LEFT JOIN EmployeeUNI on matching id.
  3. Select EmployeeUNI.unique_id (will be NULL when no match) and Employees.name.

Why it works

A LEFT JOIN preserves all rows from the left table (Employees). When there is no corresponding id in EmployeeUNI, the unique_id column becomes NULL, satisfying the requirement.

Correlated Subquery (Alternative)

You could also use a subquery to look up each unique_id:

SELECT (SELECT unique_id FROM EmployeeUNI u WHERE u.id = e.id) AS unique_id, e.name FROM Employees e;

This returns NULL when the subquery finds no match.

Step‑by‑Step Walkthrough

  1. FROM Employees
    ‑ take every employee
  2. LEFT JOIN EmployeeUNI ON Employees.id = EmployeeUNI.id
    ‑ for each employee, pull in their unique_id if it exists
  3. SELECT EmployeeUNI.unique_id, Employees.name
    ‑ output the two needed columns

For Alice (id = 1), there is no row in EmployeeUNI, so unique_id shows NULL. For Jonathan (id = 3), the join finds unique_id = 1.

Complexity Analysis

  • Time: O(n) on average for the join, where n is the number of rows in Employees (plus overhead from indexing)
  • Space: O(1) extra, output size O(n)

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

SQL Solution

SELECT EmployeeUNI.unique_id, Employees.name FROM Employees LEFT JOIN EmployeeUNI ON Employees.id = EmployeeUNI.id;

Common Mistakes

  • Using an INNER JOIN instead of LEFT JOIN (drops employees without a unique_id)
  • Forgetting to alias tables, leading to ambiguous column references
  • Selecting columns in the wrong order (they asked for unique_id first, then name)

Edge Cases

  • All employees have no unique_id → every unique_id is NULL
  • Every employee has a unique_id → behaves like an inner join
  • Single‑row tables → still returns correct mapping or NULL

Alternative Variations

  • Return only those with no unique_id (use WHERE EmployeeUNI.id IS NULL)
  • List employees in ascending order of unique_id, with NULLs first or last (add ORDER BY unique_id)
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;