출처
문제
Write a solution to report all the duplicate emails. Note that it's guaranteed that the email field is not NULL.
Return the result table in any order.
The result format is in the following example.
문제에 대한 해석
중복되는 이메일을 찾아 출력하라 | ![]() |
![]() |
풀이(MYSQL)
-- 정답1: group by
SELECT email from Person
group by email
having count(email) > 1;
이메일을 기준으로 개수를 집계하여 1개를 초화한 경우 조회
-- 정답2: 카다시안 곱
SELECT DISTINCT(p1.email) from Person p1, Person p2
where p1.id <> p2.id AND p1.email = p2.email;
각 id에 모든 이메일을 비교하여
id는 다르나 이메일은 같은 경우 조회
-- 정답3 : inner join
SELECT DISTINCT(p1.email) from
Person p1 JOIN Person p2 ON
p1.email = p2.email AND p1.id <> p2.id;
이메일이 같은거 끼리 조인하여
아이디가 다른 경우를 출력
'문제풀이 > SQL(My sql)' 카테고리의 다른 글
[문제풀이] Leet code - Game Play Analysis IV (0) | 2024.07.17 |
---|---|
[문제풀이] Leet code - Department Highest Salary (0) | 2024.07.16 |
[문제풀이] Leet code - Consecutive Numbers (0) | 2024.07.15 |
[문제풀이] Leet code - Second Highest Salary (0) | 2024.07.12 |
[문제풀이] Leet code - Restaurant Growth (0) | 2024.07.10 |