-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_196.sql
53 lines (44 loc) · 1.19 KB
/
easy_196.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
## easy: 196. Delete Duplicate Emails
Write a SQL query to **delete** all duplicate email entries in a table named Person, keeping only unique emails based on its *smallest* **Id**.
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+------------------+
**Id** is the primary key column for this table.
For example, after running your query, the above `Person` table should have the following rows:
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
+----+------------------+
**Note**:
Your output is the whole `Person` table after executing your sql. Use `delete` statement.
*/
-- SOLUTION # Write your MySQL query statement below
DELETE FROM
person
WHERE
id NOT IN (
SELECT *
FROM (
SELECT
MIN(id) as id
FROM
person
GROUP BY email) AS s
)
;
-- Another:
DELETE p1
FROM
Person p1,
Person p2
WHERE
p1.Email = p2.Email AND
p1.Id > p2.Id
;