[196] Delete Duplicate Emails

2022. 5. 28. 01:20·SQL/LeetCode

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.

2. 문제 풀이

  • SELECT문이 아닌 DELETE문을 이용해서 쿼리 짜기.
  • 가장 작은 ID값을 가지는 하나의 고유한 이메일만 테이블에 남기기
    • 중복 이메일은 삭제한다.

3. 정답 

# 서브쿼리
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)

 

두번째 풀이

with temp as (
    select email,
            min(id) as min_id
    from person
    group by email) -- 각 email별 min_id 확인 
    
delete 
from person
where id not in (select min_id
                from temp)

 

'SQL > LeetCode' 카테고리의 다른 글

[STUDY_PALN] DAY1  (0) 2022.06.04
[177] Nth Highest Salary  (0) 2022.06.03
[180] Consecutive Numbers  (0) 2022.05.29
[185] Department Top Three Salaries  (0) 2022.05.26
[184] Department Highest Salary  (0) 2022.05.22
'SQL/LeetCode' 카테고리의 다른 글
  • [177] Nth Highest Salary
  • [180] Consecutive Numbers
  • [185] Department Top Three Salaries
  • [184] Department Highest Salary
소금깨
소금깨
  • 소금깨
    고군분투 인생살이
    소금깨
  • 전체
    오늘
    어제
    • 분류 전체보기 (328)
      • SQL (271)
        • 프로그래머스 (27)
        • LeetCode (198)
        • Hacker Rank (27)
        • Solve SQL (1)
        • 개념 (15)
      • 데이터 분석 (16)
        • 참고하며 공부하기 (14)
      • 기타 (15)
        • 통계 (14)
      • 오류 (6)
      • 인생살이 (0)
        • 리뷰 (0)
        • 일기 (0)
      • 中文 (0)
      • TABLEAU (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    easy
    medium
    SQL
    시계열데이터분석
    프리미엄
    solvesql
    MySQL
    패캠챌린지
    직장인인강
    group by
    HACKER_RANK
    프로그래머스
    LeetCode
    파이썬을활용한시계열데이터분석
    패스트캠퍼스후기
    Hard
    파이썬을활용한시계열데이터분석AtoZ올인원패키지Online
    패스트캠퍼스
    직장인자기계발
    해커랭크
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
소금깨
[196] Delete Duplicate Emails
상단으로

티스토리툴바