SQL implementation of LeetCode (196. Delete duplicate mailboxes)

SQL implementation of LeetCode (196. Delete duplicate mailboxes)

[LeetCode] 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] |
+----+------------------+

This question asks us to delete duplicate mailboxes. We can first find all the non-duplicate mailboxes, then take the inverse of the number to get the duplicate mailboxes, and delete them all. So how do we find all the non-duplicate mailboxes? We can group them by mailbox, then use the Min keyword to pick out the smaller ones, and then take the complement set to delete them:

Solution 1:

DELETE FROM Person WHERE Id NOT IN
(SELECT Id FROM (SELECT MIN(Id) Id FROM Person GROUP BY Email) p);

We can also use internal intersection to associate the two tables with email addresses, and then delete the same email address with a larger ID. See the code below:

Solution 2:

DELETE p2 FROM Person p1 JOIN Person p2 
ON p2.Email = p1.Email WHERE p2.Id > p1.Id;

We can also use where to directly associate the two tables instead of Join:

Solution 3:

DELETE p2 FROM Person p1, Person p2
WHERE p1.Email = p2.Email AND p2.Id > p1.Id;

Similar topics:

Duplicate Emails

References:

https://leetcode.com/discuss/61176/simple-solution-using-a-self-join

https://leetcode.com/discuss/48403/my-answer-delete-duplicate-emails-with-double-nested-query

This is the end of this article about SQL implementation of LeetCode (196. Delete duplicate mailboxes). For more relevant SQL implementation of deleting duplicate mailboxes, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL delete duplicate email address Likou question solution process

<<:  Div nested html without iframe

>>:  CSS code for arranging photos in Moments

Recommend

Docker deployment springboot project example analysis

This article mainly introduces the example analys...

Trash-Cli: Command-line Recycle Bin Tool on Linux

I believe everyone is familiar with the trashcan,...

Join operation in Mysql

Types of joins 1. Inner join: The fields in the t...

Google Translate Tool: Quickly implement multilingual websites

Google China has released a translation tool that ...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

Simple Implementation of HTML to Create Personal Resume

Resume Code: XML/HTML CodeCopy content to clipboa...

How to monitor Windows performance on Zabbix

Background Information I've been rereading so...

Vue implements Dialog encapsulation

Table of contents Vue2 Writing Vue3 plugin versio...

Introduction to MySQL Connection Control Plugin

Table of contents 1. Introduction to the connecti...

A brief introduction to Linux environment variable files

In the Linux system, environment variables can be...

How to authorize remote connections in MySQL in Linux

Note: Other machines (IP) cannot connect to the M...

Implementation of Docker to build Zookeeper&Kafka cluster

I've been learning Kafka recently. When I was...

How to implement page jump in Vue project

Table of contents 1. Create a vue-cli default pro...

Organize the common knowledge points of CocosCreator

Table of contents 1. Scene loading 2. Find Node 1...