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

Problem Statement

You have two tables:
Employees

ColumnType
idint
namevarchar
  • id is the primary key.

EmployeeUNI

ColumnType
idint
unique_idint
  • (id, unique_id) is the primary key.

Write a query that returns each employee’s unique_id alongside their name. If an employee does not have a corresponding unique_id, show NULL. The order of rows does not matter.

Examples

Example 1
Employees:

idname
 1 Alice
 7 Bob
 11Meir
 90Winston
 3 Jonathan
EmployeeUNI:
idunique_id
---------------
 3  1
 11 2
 90 3
Output
unique_idname
---------------------
NULLAlice
NULLBob
 2Meir
 3Winston
 1Jonathan

Example 2
Employees:

idname
 1 Tom
 2 Jane
EmployeeUNI: (empty)
Output
unique_idname
-----------------
NULLTom
NULLJane

Example 3
Employees:

idname
 5 Richard
 6 Samantha
EmployeeUNI:
idunique_id
---------------
 5  10
 6  20
Output
unique_idname
----------------------
 10Richard
 20Samantha

Constraints

  • Each id in Employees is unique.
  • Each row in EmployeeUNI maps an existing employee to exactly one unique_id.
  • Tables may each contain up to a few thousand rows.
  • You may return results in any order.

Hints

  1. Which type of JOIN returns all rows from Employees even when there’s no matching row in EmployeeUNI?
  2. How does SQL represent missing values when a join has no match?

Solution Approach

We need every employee, regardless of whether they have a unique identifier. An INNER JOIN would drop employees without a match. A LEFT JOIN preserves all employees and yields NULL for missing unique_ids.

SQL Query

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

Explanation

  1. FROM Employees AS e
    Start with Employees as the left table alias e.
  2. LEFT JOIN EmployeeUNI AS u ON e.id = u.id
    Match each employee’s id to EmployeeUNI.id.
    • If a match exists, u.unique_id is filled.
    • Otherwise, u.unique_id is NULL.
  3. SELECT u.unique_id, e.name
    Output the unique identifier first, then the employee’s name.

Complexity Analysis

  • Time: O(N + M) for scanning both tables and performing the join (N = #Employees, M = #EmployeeUNI).
  • Space: O(N + M) for any intermediate join structures.

Step‑by‑Step Walkthrough

Given
Employees:

idname
 1 Alice
 2 Bob
EmployeeUNI:
idunique_id
---------------
 2  42
  • Step 1: Read Employees row (1, Alice). No EmployeeUNI row with id=1 → unique_id becomes NULL.
  • Step 2: Read Employees row (2, Bob). Find EmployeeUNI row with id=2 → unique_id=42.
  • Result:
    unique_idname
    NULLAlice
     42Bob

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Common Mistakes

  • Using INNER JOIN (drops employees without a unique_id).
  • Swapping column order (selecting name before unique_id).
  • Forgetting to alias tables, leading to ambiguous column references.

Edge Cases

  • No Employees: returns an empty result set.
  • No EmployeeUNI entries: every unique_id is NULL.
  • Every employee has a unique_id: same as an INNER JOIN result.

Alternative Variations

  • Right Join (in SQL dialects that support it): swap table positions.
  • Full Outer Join to find employees without unique_ids and unique_ids without employees (if data were inconsistent).
  • Using COALESCE(u.unique_id, 0) to replace NULLs with a default value like 0.
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
1353. Maximum Number of Events That Can Be Attended - Detailed Explanation
Learn to solve Leetcode 1353. Maximum Number of Events That Can Be Attended with multiple approaches.
185. Department Top Three Salaries - Detailed Explanation
Learn to solve Leetcode 185. Department Top Three Salaries with multiple approaches.
242. Valid Anagram - Detailed Explanation
Learn to solve Leetcode 242. Valid Anagram with multiple approaches.
2558. Take Gifts From the Richest Pile - Detailed Explanation
Learn to solve Leetcode 2558. Take Gifts From the Richest Pile with multiple approaches.
1014. Best Sightseeing Pair - Detailed Explanation
Learn to solve Leetcode 1014. Best Sightseeing Pair with multiple approaches.
43. Multiply Strings - Detailed Explanation
Learn to solve Leetcode 43. Multiply Strings with multiple approaches.
Related Courses
Course image
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.
4.6
Discounted price for Your Region

$197

Course image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$78

Course image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Image
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.