Coding Challenges/LeetCode

[SQL50] 1378. Replace Employee ID With The Unique Identifier

기록해연 2025. 2. 11. 09:51

난이도: EASY


문제

더보기

Table: Employees
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| name          | varchar |
+---------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains the id and the name of an employee in a company.
 

Table: EmployeeUNI
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| unique_id     | int     |
+---------------+---------+
(id, unique_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id and the corresponding unique id of an employee in the company.
 
Write a solution to show the unique ID of each user, If a user does not have a unique ID replace just show null.
Return the result table in any order.
The result format is in the following example.

Example 1:
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 |
+-----------+----------+

Explanation: 
Alice and Bob do not have a unique ID, We will show null instead.
The unique ID of Meir is 2.
The unique ID of Winston is 3.
The unique ID of Jonathan is 1.


 

시도 1

SELECT B.unique_id, A.name
FROM Employees A LEFT OUTER JOIN EmployeeUNI B ON A.id = B.id
ORDER BY A.id DESC

any order라서 여기서 ORDER BY 빼서 재시도함(속도를 조금이라도 올려보고자 하는 노오력)


 

나의 최종 제출 답안:

SELECT B.unique_id, A.name
FROM Employees A LEFT OUTER JOIN EmployeeUNI B ON A.id = B.id

 

 


 

chatGPT에게 더 간단하고 효율적이게 수정해달라고 요청한 답안: (근데 별 도움은 안되는듯)

 

✅ 최적화된 SQL 쿼리

SELECT COALESCE(B.unique_id, NULL) AS unique_id, A.name
FROM Employees A 
LEFT JOIN EmployeeUNI B 
ON A.id = B.id;

 

🔹 개선된 점

  1. COALESCE(B.unique_id, NULL)는 사실상 필요 없지만 가독성을 높여줌.
    • LEFT JOIN을 사용하면 B.unique_id가 없을 때 NULL이 자동으로 들어감.
    • 하지만 COALESCE()를 명확하게 사용하면 읽기 쉬운 코드가 됨.
  2. LEFT JOIN이 아닌 OUTER JOIN은 불필요
    • LEFT JOIN과 LEFT OUTER JOIN은 동일하게 작동하므로, OUTER 키워드를 제거해도 무방함.

✅ 더 빠르게 실행하려면? (인덱스 최적화)

이 SQL 쿼리를 실행할 때 JOIN이 빠르게 실행되도록 인덱스를 추가하면 성능이 향상될 수 있음.

CREATE INDEX idx_employee_id ON Employees(id);
CREATE INDEX idx_employeeuni_id ON EmployeeUNI(id);

 

🔹 인덱스를 추가하는 이유

  • JOIN이 실행될 때 A.id = B.id 조건을 빠르게 찾을 수 있도록 최적화됨.
  • 특히 데이터가 많을 경우, 해시 조인(Hash Join) 또는 병합 조인(Merge Join)을 사용할 수 있어 속도가 향상됨.

'Coding Challenges > LeetCode' 카테고리의 다른 글

[SQL50] 1068. Product Sales Analysis I  (0) 2025.02.11
[Java] 27. Remove Element  (0) 2025.02.11
[Java] 26. Remove Duplicates from Sorted Array  (0) 2025.02.10
[SQL50] 1148. Article Views I  (0) 2025.02.10
[SQL50] 1683. Invalid Tweets  (0) 2025.02.10