-
Notifications
You must be signed in to change notification settings - Fork 0
/
196.delete-duplicate-emails.sql
56 lines (54 loc) · 1.38 KB
/
196.delete-duplicate-emails.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
54
55
--
-- @lc app=leetcode id=196 lang=mysql
--
-- [196] Delete Duplicate Emails
--
-- https://leetcode.com/problems/delete-duplicate-emails/description/
--
-- database
-- Easy (37.38%)
-- Likes: 359
-- Dislikes: 487
-- Total Accepted: 94.9K
-- Total Submissions: 252.4K
-- Testcase Example: '{"headers": {"Person": ["Id", "Email"]}, "rows": {"Person": [[1, "[email protected]"], [2, "[email protected]"], [3, "[email protected]"]]}}'
--
-- 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.
--
--
-- @lc code=start
# Write your MySQL query statement below
DELETE p1 FROM Person p1,
Person p2
WHERE
p1.Email = p2.Email AND p1.Id > p2.Id
-- @lc code=end