1.题目
Write a SQL query to find all duplicate emails in a table named Person
.
翻译:写一个SQL查询语句,找出Person表中所有重复的Email。
+----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+
For example, your query should return the following for the above table:
+---------+ | Email | +---------+ | a@b.com | +---------+
2.思路
首先,将Person表按Email进行分组(Email一致归为一组),
然后,筛选出其中组内元素大于1的不同的Email(使用distinct)
3.算法
select distinct Email from Person group by Email having count(Email)>1