Skip to content

Commit 84e14dc

Browse files
Add SQL solutions to LC196 and LC182
1 parent eceb878 commit 84e14dc

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

LC182-Duplicate-Emails

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Write a SQL query to find all duplicate emails in a table named Person.
3+
4+
+----+---------+
5+
| Id | Email |
6+
+----+---------+
7+
| 1 | a@b.com |
8+
| 2 | c@d.com |
9+
| 3 | a@b.com |
10+
+----+---------+
11+
For example, your query should return the following for the above table:
12+
13+
+---------+
14+
| Email |
15+
+---------+
16+
| a@b.com |
17+
+---------+
18+
*/
19+
20+
# Write your MySQL query statement below
21+
SELECT DISTINCT p1.Email
22+
FROM Person p1,
23+
Person p2
24+
WHERE p1.Email = p2.Email AND p1.Id != p2.Id;

LC196-Delete-Duplicate-Emails

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
3+
4+
+----+------------------+
5+
| Id | Email |
6+
+----+------------------+
7+
| 1 | john@example.com |
8+
| 2 | bob@example.com |
9+
| 3 | john@example.com |
10+
+----+------------------+
11+
Id is the primary key column for this table.
12+
For example, after running your query, the above Person table should have the following rows:
13+
14+
+----+------------------+
15+
| Id | Email |
16+
+----+------------------+
17+
| 1 | john@example.com |
18+
| 2 | bob@example.com |
19+
+----+------------------+
20+
Note:
21+
22+
Your output is the whole Person table after executing your sql. Use delete statement.
23+
*/
24+
25+
DELETE p1 FROM Person p1,
26+
Person P2
27+
WHERE
28+
p1.Email = p2.Email AND p1.Id > p2.Id;

0 commit comments

Comments
 (0)