상세 컨텐츠

본문 제목

Delete Duplicate Emails(Leet Code)

SQL/MySQL 문제풀이

by 관재탑 2022. 8. 2. 21:03

본문

https://leetcode.com/problems/delete-duplicate-emails/

 

Delete Duplicate Emails - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

문제

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.

 

Write an SQL query to delete all the duplicate emails, keeping only one unique email with the smallest id. Note that you are supposed to write a DELETE statement and not a SELECT one.

After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.

The query result format is in the following example.

 

Example 1:

Input: 
Person table:
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Output: 
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+
Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.

 

Write an SQL query to delete all the duplicate emails, keeping only one unique email with the smallest id. Note that you are supposed to write a DELETE statement and not a SELECT one.

After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.

The query result format is in the following example.

 

Example 1:

Input: 
Person table:
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Output: 
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+
Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.

 

 

정답 

DELETE
FROM person
WHERE id NOT IN(
SELECT sub.min_id 
FROM (
SELECT email, MIN(id) AS min_id
FROM Person
GROUP BY email
) sub)

 

 

해설 

FROM절에 서브쿼리 작성시 별칭을 줘야한다. 

  1. 먼저 이메일별로 GROUP BY한 다음에 최소 ID를 구한다.
  2. 그 다음에 이메일별 최소 아이디를 SELECT한다.
  3. 이메일별 최소아이디에 포함 되지 않은 ROW를 DELETE한다.

'SQL > MySQL 문제풀이' 카테고리의 다른 글

challenges(HackerRank)  (0) 2022.08.05
Department Highest Salary(Leet Code)  (0) 2022.08.05
Swap Salary(LeetCode)  (0) 2022.08.02
Duplicate Emails(LeetCode)  (0) 2022.07.30
Symmetric Pairs(HackerRank)  (0) 2022.07.20

관련글 더보기

댓글 영역