182.Duplicate Emalis

https://leetcode.com/problems/duplicate-emails/

문제설명

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 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 query result format is in the following example.

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.

Email중에서 중복되는 것을 조회한다.


풀이

SELECT email AS "Email"
FROM Person
GROUP BY email
HAVING count(email) >= 2;

email의 count 갯수가 2이상인 것으로 간단히 필터링 가능하다.

다른풀이

#1.
SELECT e.Name AS Employee
FROM Employee e
JOIN Employee m ON e.ManagerId = m.Id
WHERE e.Salary > m.Salary

#2.
select DISTINCT email from Person group by email having count(email) > 1;