我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/duplicate-emails/description/
题目描述:
知识点:GROUP BY子句和HAVING子句
思路一:使用子查询
时间复杂度是O(n ^ 2),其中n为Email表中的记录数。
SQL语句:
SELECT DISTINCT Email FROM Person AS t1 WHERE (SELECT COUNT(1) FROM Person AS t2 WHERE t2.Email = t1.Email) > 1;
LeetCode解题报告:
思路二:利用GROUP BY子句生成临时表
时间复杂度是O(n),其中n为Email表中的记录数。
SQL语句:
SELECT Email FROM (SELECT Email, COUNT(1) AS Num FROM Person GROUP BY Email) AS STATISTIC WHERE Num > 1;
LeetCode解题报告:
思路三:GROUP BY子句和HAVING子句
时间复杂度是O(n),其中n为Email表中的记录数。
SQL语句:
SELECT Email FROM Person GROUP BY Email HAVING COUNT(1) > 1;
LeetCode解题报告: