You have two tables: Employees and EmployeeUNI.
The Employees table contains employee information with columns:
id(int) - Primary key for employee IDname(varchar) - Employee name
The EmployeeUNI table contains unique identifier mappings with columns:
id(int) - Employee ID (references Employees.id)unique_id(int) - Corresponding unique identifier
Task: Write a SQL query to show the unique_id and name for each employee. If an employee doesn't have a unique ID in the EmployeeUNI table, show null for their unique_id.
Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key for employee ID |
name
|
varchar | Employee name |
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Employee ID (references Employees.id) |
unique_id
PK
|
int | Corresponding unique identifier |
Input & Output
| id | name |
|---|---|
| 1 | Alice |
| 7 | Bob |
| 11 | Meir |
| 90 | Winston |
| id | unique_id |
|---|---|
| 1 | 22 |
| 7 | 13 |
| 11 | 90 |
| unique_id | name |
|---|---|
| 22 | Alice |
| 13 | Bob |
| 90 | Meir |
| Winston |
The LEFT JOIN matches employees with their unique IDs. Alice (id=1) gets unique_id=22, Bob (id=7) gets unique_id=13, Meir (id=11) gets unique_id=90. Winston (id=90) has no matching unique_id, so it shows null.
| id | name |
|---|---|
| 1 | John |
| 2 | Jane |
| id | unique_id |
|---|---|
| 1 | 100 |
| 2 | 200 |
| unique_id | name |
|---|---|
| 100 | John |
| 200 | Jane |
When all employees have corresponding unique IDs, the LEFT JOIN works like an INNER JOIN, returning all employees with their unique identifiers.
| id | name |
|---|---|
| 5 | Sarah |
| 6 | Mike |
| id | unique_id |
|---|
| unique_id | name |
|---|---|
| Sarah | |
| Mike |
When no employees have unique IDs in the EmployeeUNI table, all employees are still returned with null values for unique_id.
Constraints
-
1 ≤ Employees.id ≤ 10^5 -
1 ≤ EmployeeUNI.unique_id ≤ 10^5 -
Each row in EmployeeUNI has a unique (id, unique_id) combination