AskHandle

AskHandle Blog

How Can I Delete Duplicate Emails in SQL?

April 11, 2025Katherine Holland3 min read

How Can I Delete Duplicate Emails in SQL?

In many database scenarios, you may encounter duplicate entries that need to be dealt with, especially when it comes to user email addresses. Maintaining a clean database is crucial for operational efficiency, and deleting duplicates is an important task. This article explains how to efficiently remove duplicate email entries from a SQL database.

Let’s consider a hypothetical table structure named users that contains user information, including an email column.

sql
1CREATE TABLE users (
2    id INT PRIMARY KEY,
3    email VARCHAR(255)
4);

For illustration, assume we have the following data in the users table:

text
1id | email
2---|-------------------
31  | example@example.com
42  | test@test.com
53  | example@example.com
64  | hello@world.com
75  | test@test.com

As we can see, there are duplicates in the email column. The goal is to remove these duplicates while keeping one instance of each unique email.

Step 1: Identify Duplicates

Before deleting duplicates, it is useful to find out which emails are duplicated. The following SQL query will help you identify these duplicates:

sql
1SELECT email, COUNT(*) as count
2FROM users
3GROUP BY email
4HAVING COUNT(*) > 1;

This query groups the records by the email column and counts how many times each email appears. The HAVING clause filters the results to show only those emails that appear more than once.

Step 2: Delete Duplicates

One common approach to deleting duplicates is to keep the entry with the lowest id and remove the rest. You can achieve this with a Common Table Expression (CTE) or a subquery. Here’s an example using a CTE:

sql
1WITH CTE AS (
2    SELECT id,
3           email,
4           ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) as row_num
5    FROM users
6)
7DELETE FROM CTE
8WHERE row_num > 1;

In this query, the CTE selects each email and assigns a sequential number (row_num) to each duplicate email based on the id ordering. The DELETE statement then removes all records with a row_num greater than 1, which are the duplicates.

Alternative Method: Using a Subquery

If you're more comfortable using a subquery rather than a CTE, consider this alternative approach:

sql
1DELETE FROM users
2WHERE id NOT IN (
3    SELECT MIN(id)
4    FROM users
5    GROUP BY email
6);

This query deletes entries from the users table where the id is not one of the minimum IDs for each unique email, effectively retaining the entry with the lowest ID for each email address.

Important Note

When deleting duplicates, always be cautious and back up your data. It is vital to ensure data integrity throughout the operation. Testing your SQL queries in a development environment before applying them to production is a prudent practice.

By following these steps, you can effectively manage and delete duplicate emails in your SQL database, maintaining a clean and organized dataset.