SQL implementation of LeetCode (182. Duplicate mailboxes)

SQL implementation of LeetCode (182. Duplicate mailboxes)

[LeetCode] 182.Duplicate Emails

Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email |
+---------+
| [email protected] |
+---------+

Note : All emails are in lowercase.

This question asks us to find duplicate email addresses, so the most direct way is to use the fixed combination of Group by...Having Count(*)..., the code is as follows:

Solution 1:

SELECT Email FROM Person GROUP BY Email
HAVING COUNT(*) > 1;

We can also use internal intersection to do this, using Email to internally intersect the two tables, and then return rows with different IDs, which means that two different people use the same email address:

Solution 2:

SELECT DISTINCT p1.Email FROM Person p1
JOIN Person p2 ON p1.Email = p2.Email
WHERE p1.Id <> p2.Id;

References:

https://leetcode.com/discuss/53206/a-solution-using-a-group-by-and-another-one-using-a-self-join

This is the end of this article about SQL implementation of LeetCode (182. Duplicate mailboxes). For more relevant SQL implementation of 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 implementation of LeetCode (196. Delete duplicate mailboxes)
  • SQL implementation LeetCode (185. Top three highest salaries in the department)
  • SQL implementation of LeetCode (184. The highest salary in the department)
  • SQL implementation of LeetCode (183. Customers who have never placed an order)
  • SQL implementation of LeetCode (181. Employees earn more than managers)
  • SQL implements LeetCode (180. Continuous numbers)
  • C++ implementation of LeetCode (179. Maximum number of combinations)
  • SQL implementation of LeetCode (197. Rising temperature)

<<:  CSS setting div background image implementation code

>>:  Example of using #include file in html

Recommend

Solution to MySQL unable to read table error (MySQL 1018 error)

1. Error reproduction I can access the MySQL data...

Detailed explanation of Nginx regular expressions

Nginx (engine x) is a high-performance HTTP and r...

Simple writing of MYSQL stored procedures and functions

What is a stored procedure Simply put, it is a se...

Summary of Problems in Installing MySQL 5.7.19 under Linux

The first time I installed MySQL on my virtual ma...

Solution to the conflict between Linux kernel and SVN versions

Phenomenon The system could compile the Linux sys...

Tips for data statistics in MySQL

As a commonly used database, MySQL requires a lot...

Ubuntu 18.04 disable/enable touchpad via command

In Ubuntu, you often encounter the situation wher...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...

Summary of the differences between MySQL storage engines MyISAM and InnoDB

1. Changes in MySQL's default storage engine ...

Sample code for implementing music player with native JS

This article mainly introduces the sample code of...

HTML uses regular expressions to test table examples

Here is an example code for using regular express...