LeetCode 182. Duplicate Emails
LeetCode 182. Duplicate Emails
題目
Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| 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 |
+---------+
Note: All emails are in lowercase.
題目大意:編寫一個SQL查詢從Person表中找出所有重複的郵箱地址。
解題思路
- GROUP BY HAVING
SELECT `Email` FROM `Person` GROUP BY `Email` HAVING COUNT(*) > 1 ```
* 做笛卡爾積
* DISTINCT
```SQL
SELECT DISTINCT t1.`Email` FROM `Person` AS t1, `Person` AS t2 WHERE t1.id != t2.id and t1.`Email` = t2.`Email` ```
* INNER JOIN
SQL
SELECT DISTINCT p.Email FROM Person p INNER JOIN Person q ON p.Id != q.Id AND p.Email = q.Email;
*測試下來使用group by 會更快一點*
## GROUP BY HAVING 官方文件
[GROUP BY](https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html) 資料分組(聚合)
[HAVING](https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html) 資料聚合後的篩選
相關文章
- [LeetCode] Find the Duplicate NumberLeetCode
- LeetCode-Find the Duplicate NumberLeetCode
- LeetCode-Remove Duplicate LettersLeetCodeREM
- leetcode287: Find the Duplicate NumberLeetCode
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- LeetCode-Contains Duplicate IILeetCodeAI
- LeetCode 題解(252) : Find the Duplicate NumberLeetCode
- leetcode 219. Contains Duplicate IILeetCodeAI
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- 217. Contains Duplicate--LeetCode RecordAILeetCode
- duplicate databaseDatabase
- LeetCode C++ 316. Remove Duplicate Letters【Stack/Greedy/String】中等LeetCodeC++REM
- RMAN duplicate databaseDatabase
- yum error - package is a duplicate withErrorPackage
- DUPLICATE DATABASE 注意事項Database
- 通過rman duplicate database!Database
- RMAN duplicate On Windows7Windows
- rman duplicate操作手冊
- Duplicate的一點總結
- Oracle DG搭建1(duplicate方式)Oracle
- Oracle 11G Duplicate DatabaseOracleDatabase
- RMAN duplicate database到新主機Database
- 使用duplicate克隆資料庫資料庫
- mysql INSERT ... ON DUPLICATE KEY UPDATEMySql
- 錯誤:duplicate column name: picstitle
- Oracle rman duplicate遷移測試Oracle
- MySQL_插入更新 ON DUPLICATE KEY UPDATEMySql
- RMAN DUPLICATE建立DataGuard物理備庫
- rman duplicate搭建第二個 dg
- 單例項的duplicate(non ASM)單例ASM
- 使用RMAN建立Duplicate資料庫資料庫
- rman duplicate clone庫的尷尬
- 【轉】RMAN建立duplicate資料庫資料庫
- 使用RMAN duplicate對源庫的某個incarnation進行duplicate操作時遇到的問題
- Oracle 11GR2 Duplicate from BackupOracle
- Tensorboard: ValueError: Duplicate plugins for name projectorORBErrorPluginProject
- 解決 Inkscape 報錯 Duplicate 問題
- MySQL insert on duplicate key update 死鎖MySql