DuplicateEmails
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.
主要是group by的應用
select Email from ( select Email,count(Email) as cnt from Person group by Email ) t where cnt>1;
或者
select Email from ( select Email,count(Email) as cnt from Person group by Email having cnt>1) t ;
order by 要在group by 之後使用,不能在group by之前使用。
select Email from ( select Email,count(Email) as cnt from Person order by Email group by Email ) t where cnt>1;
這個執行會有問題。