8000 Initial Commit · priyesh0453/SQL-LeetCode@05dc22c · GitHub
[go: up one dir, main page]

Skip to content

Commit 05dc22c

Browse files
committed
Initial Commit
1 parent 6241364 commit 05dc22c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Easy/196. Delete Duplicate Emails.sql

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @ 8000 @
1+
/*
2+
Table: Person
3+
4+
+-------------+---------+
5+
| Column Name | Type |
6+
+-------------+---------+
7+
| id | int |
8+
| email | varchar |
9+
+-------------+---------+
10+
id is the primary key column for this table.
11+
Each row of this table contains an email. The emails will not contain uppercase letters.
12+
13+
14+
Required Query:
15+
16+
1) Delete all the duplicate emails, keeping only one unique email with the smallest id. Note that you are supposed to write a DELETE statement and not a SELECT one.
17+
18+
2) After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.
19+
20+
SQL Schema:
21+
22+
Create table If Not Exists Person (Id int, Email varchar(255))
23+
Truncate table Person
24+
insert into Person (id, email) values ('1', 'john@example.com')
25+
insert into Person (id, email) values ('2', 'bob@example.com')
26+
insert into Person (id, email) values ('3', 'john@example.com')
27+
28+
Example:
29+
30+
Input:
31+
Person table:
32+
+----+------------------+
33+
| id | email |
34+
+----+------------------+
35+
| 1 | john@example.com |
36+
| 2 | bob@example.com |
37+
| 3 | john@example.com |
38+
+----+------------------+
39+
Output:
40+
+----+------------------+
41+
| id | email |
42+
+----+------------------+
43+
| 1 | john@example.com |
44+
| 2 | bob@example.com |
45+
+----+------------------+
46+
*/
47+
48+
-- Solution:
49+
DELETE duplicate
50+
FROM Person original JOIN Person duplicate
51+
ON original.email = duplicate.email AND original.id < duplicate.id

0 commit comments

Comments
 (0)
0