Post

[LeetCode 182] Duplicate Emails

LeetCode 182 (SQL)
[Duplicate Emails] 문제 풀이

[LeetCode 182] Duplicate Emails

문제 바로가기


Description


Table: Person

1
2
3
4
5
6
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+

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.

Return the result table in any order.

The result format is in the following example.


Example 1


  • Input:
    • Person table:
      1
      2
      3
      4
      5
      6
      7
      
      +----+---------+
      | id | email   |
      +----+---------+
      | 1  | a@b.com |
      | 2  | c@d.com |
      | 3  | a@b.com |
      +----+---------+
      
  • Output:
    1
    2
    3
    4
    5
    
    +---------+
    | Email   |
    +---------+
    | a@b.com |
    +---------+
    
  • Explanation: a@b.com is repeated two times.


Constraints








Code


내 제출


1
2
3
4
5
6
# Write your MySQL query statement below

SELECT email As Email 
FROM Person 
GROUP BY email 
HAVING COUNT(email) >= 2;


RuntimeMemory
355 ms0.0 B


다른 풀이


1
2
3
4
5
6
-- # Write your MySQL query statement below
-- select p.email from Person p where  exists (select 1 from Person p1 where p1.email=p.email AND p1.id!=p.id)
select p.email 
from Person p 
group by p.email 
having count(*)>1;


Reference


This post is licensed under CC BY 4.0 by the author.