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

MySQL sequence AUTO_INCREMENT detailed explanation and example code

MySQL sequence AUTO_INCREMENT detailed explanatio...

LinkedIn revamps to simplify website browsing

Business social networking site LinkedIn recently...

Function overloading in TypeScript

Table of contents 1. Function signature 2. Functi...

A possible bug when MySQL executes the sum function on the window function

When using MySql's window function to collect...

What are the image file formats and how to choose

1. Which three formats? They are: gif, jpg, and pn...

Basic concepts and common methods of Map mapping in ECMAScript6

Table of contents What is a Mapping Difference be...

Detailed explanation of identifying files with the same content on Linux

Preface Sometimes file copies amount to a huge wa...

Javascript basics about built-in objects

Table of contents 1. Introduction to built-in obj...

Docker data volume common operation code examples

If the developer uses Dockerfile to build the ima...

Summary of Binlog usage of MySQL database (must read)

I won't go into details about how important b...

CSS horizontal centering and limiting the maximum width

A CSS layout and style question: how to balance h...

How to manually encapsulate paging components in Vue3.0

This article shares the specific code of the vue3...

Detailed explanation of how to quickly build a blog website using Docker

Table of contents 1. Preparation 2. Deployment Pr...