[LeetCode] 196.Delete Duplicate EmailsWrite a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
For example, after running your query, the above Person table should have the following rows:
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:
|
<<: Div nested html without iframe
>>: CSS code for arranging photos in Moments
This article mainly introduces the example analys...
I believe everyone is familiar with the trashcan,...
Types of joins 1. Inner join: The fields in the t...
Google China has released a translation tool that ...
1. Virtual environment virtualenv installation 1....
Resume Code: XML/HTML CodeCopy content to clipboa...
Background Information I've been rereading so...
Table of contents Vue2 Writing Vue3 plugin versio...
Table of contents 1. Introduction to the connecti...
In the Linux system, environment variables can be...
Note: Other machines (IP) cannot connect to the M...
I've been learning Kafka recently. When I was...
Table of contents 1. Create a vue-cli default pro...
The environment of this article is Windows 10, an...
Table of contents 1. Scene loading 2. Find Node 1...