Data & BI/SQL Coding Test

[LeetCode](초급) 182. Duplicate Emails

무료한하늘 2025. 5. 16. 11:10
728x90

문제

Column Name Type
id int
email varchar
[Table : Person]
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 report all the duplicate emails. Note that it's guaranteed that the email field is not NULL.

주어진 테이블에서 중복된 이메일을 출력해라.

 

풀이

SELECT DISTINCT(ABC.email)
FROM
    (SELECT email, COUNT(email) as cnt
    FROM Person
    GROUP BY email) AS ABC
WHERE ABC.cnt > 1

 

주어진 테이블(Person)에서 email을 기준으로 그룹화하여 개수를 담고 있는 서브쿼리를 만든다.

그리고 그 테이블에서 이메일을 중복제거하여 개수가 1 초과인 것을 조회한다.

300x250