题目:
Write a SQL query to find all duplicate emails in a table named Person.
+—-+———+
| Id | Email |
+—-+———+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+—-+———+
For example, your query should return the following for the above table:
+———+
| Email |
+———+
| [email protected] |
+———+
Note: All emails are in lowercase.
Answer:
思路:GROUP BY
和HAVING COUNT
用法。
SELECT Email FROM person GROUP BY Email HAVING COUNT(Email) > 1
附表email
的sql:
Create table If Not Exists Person (Id int, Email varchar(255));
Truncate table Person;
insert into Person (Id, Email) values ('1', '[email protected]');
insert into Person (Id, Email) values ('2', '[email protected]');
insert into Person (Id, Email) values ('3', '[email protected]');