Interview Questions/SQL/Replace Employee ID with Unique Identifier

Replace Employee ID with Unique Identifier

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have two tables: Employees and EmployeeUNI. Each employee has an id and a name. Some employees have been assigned a unique_id in the EmployeeUNI table, but not all. Write a SQL query to show the unique_id and name of each employee. If an employee does not have a unique_id, show NULL instead. Return the result in any order. (id, unique_id) is the primary key of EmployeeUNI.

Database Schema

Employees

Column NameTypeDescription
idINTPrimary key
nameVARCHAREmployee name

EmployeeUNI

Column NameTypeDescription
idINTFK to Employees
unique_idINTUnique identifier assigned

Example

Employees

idname
1Alice
7Bob
11Meir
90Winston
3Jonathan

EmployeeUNI

idunique_id
31
112
903

Output

unique_idname
NULLAlice
NULLBob
2Meir
3Winston
1Jonathan

Explanation:

Alice and Bob have no unique_id - show NULL. Meir → 2, Winston → 3, Jonathan → 1.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Employees

idname
1Alice
7Bob
11Meir
90Winston
3Jonathan

EmployeeUNI

idunique_id
31
112
903

Output

unique_idname
NULLAlice
NULLBob
2Meir
3Winston
1Jonathan