Skip to content

Commit b742d55

Browse files
Replace Employee ID With The Unique Identifier
1 parent 4fbf83a commit b742d55

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Leetcode_1378.sql

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Replace Employee ID With The Unique Identifier
2+
3+
4+
Table: Employees
5+
+---------------+---------+
6+
| Column Name | Type |
7+
+---------------+---------+
8+
| id | int |
9+
| name | varchar |
10+
+---------------+---------+
11+
id is the primary key for this table.
12+
Each row of this table contains the id and the name of an employee in a company.
13+
14+
Table: EmployeeUNI
15+
+---------------+---------+
16+
| Column Name | Type |
17+
+---------------+---------+
18+
| id | int |
19+
| unique_id | int |
20+
+---------------+---------+
21+
(id, unique_id) is the primary key for this table.
22+
Each row of this table contains the id and the corresponding unique id of an employee in the company.
23+
24+
Write an SQL query to show the unique ID of each user, If a user doesn't have a unique ID replace just show null.
25+
Return the result table in any order.
26+
27+
The query result format is in the following example:
28+
29+
Employees table:
30+
+----+----------+
31+
| id | name |
32+
+----+----------+
33+
| 1 | Alice |
34+
| 7 | Bob |
35+
| 11 | Meir |
36+
| 90 | Winston |
37+
| 3 | Jonathan |
38+
+----+----------+
39+
40+
EmployeeUNI table:
41+
+----+-----------+
42+
| id | unique_id |
43+
+----+-----------+
44+
| 3 | 1 |
45+
| 11 | 2 |
46+
| 90 | 3 |
47+
+----+-----------+
48+
49+
EmployeeUNI table:
50+
+-----------+----------+
51+
| unique_id | name |
52+
+-----------+----------+
53+
| null | Alice |
54+
| null | Bob |
55+
| 2 | Meir |
56+
| 3 | Winston |
57+
| 1 | Jonathan |
58+
+-----------+----------+
59+
60+
Alice and Bob don't have a unique ID, We will show null instead.
61+
The unique ID of Meir is 2.
62+
The unique ID of Winston is 3.
63+
The unique ID of Jonathan is 1.
64+
*/
65+
66+
# MY SOLUTION
67+
SELECT unique_id, name
68+
FROM Employees e
69+
LEFT JOIN EmployeeUNI uni
70+
ON e.id = uni.id

0 commit comments

Comments
 (0)