LeetCode MySQL-182. Duplicate Emails

    LeetCode MySQL

    Write an SQL query to report all the duplicate emails.
    Return the result table in any order

    寫一個 SQL Query來報告有重複的信件。
    可任意排序結果。
    

    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.
    

    Example 1:

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

    Solution:
    1. 確認標題 email 為 Email 。
    2. 來自 Person 的 table。
    3. 以 email 為群組。
    4. 有 Email 次數大於1。

    Code.1:

    SELECT email AS Email
      FROM Person
    GROUP BY email
    HAVING COUNT(Email) > 1;
    

    Code.2:

    SELECT email AS Email
      FROM Person
    GROUP BY email
    HAVING COUNT(*) > 1;
    

    newEmail