Replace Employee ID With The Unique Identifier
Task: Write a solution to show the unique ID of each user.
Table: Employees
+-------------+-------------+
| Column Name | Type |
+-------------+-------------+
| id | INT |
| name | varchar(20) |
+-------------+-------------+
Table: EmployeeUNI
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | INT |
| unique_id | INT |
+-------------+------+
Input: Employees table:
+----+----------+
| id | name |
+----+----------+
| 1 | Alice |
| 7 | Bob |
| 11 | Meir |
| 90 | Winston |
| 3 | Jonathan |
+----+----------+
EmployeeUNI table:
+----+-----------+
| id | unique_id |
+----+-----------+
| 3 | 1 |
| 11 | 2 |
| 90 | 3 |
+----+-----------+
Output:
+-----------+----------+
| unique_id | name |
+-----------+----------+
| null | Alice |
| null | Bob |
| 2 | Meir |
| 3 | Winston |
| 1 | Jonathan |
+-----------+----------+ View Reference Solution & Explanation ▼
This solution uses a `JOIN` to combine data from multiple tables to find the required output.
SELECT eu.unique_id, e.name FROM Employees e LEFT JOIN EmployeeUNI eu ON e.id = eu.id;