본문 바로가기
[업무 지식]/MySQL

[delete] Delete Duplicate Emails

by 에디터 윤슬 2024. 11. 14.

링크

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

문제

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.

정답

with soso as
(
    select min(id) as min_id
    from person
    group by email
)
delete
from person
where id not in (select min_id from soso)

해설

  • delete 문법
delete
from table_name
where column = value

DELETE 문은 해당 테이블에서 WHERE 절의 조건을 만족하는 레코드만을 삭제
즉, 테이블에서 명시된 필드와, 그 값이 일치하는 레코드만을 삭제
만약 WHERE 절을 생략하면, 해당 테이블에 저장된 모든 데이터가 삭제