리트코드(leetcode) 문제풀이 : Delete Duplicate Emails (10/18)

백엔ㄷ현·2024년 10월 18일

Table: Person

Column NameType
idint
emailvarchar

id is the primary key (column with unique values) for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.

Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id.

For SQL users, please note that you are supposed to write a DELETE statement and not a SELECT one.

For Pandas users, please note that you are supposed to modify Person in place.

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 result format is in the following example.

Example 1:

Input:
Person table:

idemail
1john@example.com
2bob@example.com
3john@example.com

Output:

idemail
1john@example.com
2bob@example.com

Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.




id 가 가장 작은 고유 이메일을 하나만 보관하고 중복 이메일을 삭제하는 요구 사항이 있는 문제이다. 조건에 특이한 점이 select 대신에 delete 를 사용하여 문제를 풀어야한다.
delete 문은 공부만 해봤지 처음 써보았는데 select 문이랑 차이가 없는 것 같았다.
조회를 할 컬럼들 대신에 삭제 할 컬럼을 조건에 맞게 써주면 간단하게 풀리는 문제였다.

delete p1
from person p1
join person p2
	on p1.email = p2.email and p1.id > p2.id;
profile
매일매일 공부한 내용과 코드 기록하겠습니다

0개의 댓글