Wednesday, January 31, 2024

How to remove duplicate values from a table in SQL? Example

In the world of data management, duplicate values can be a persistent nuisance. Duplicate records not only clutter your database but can also lead to erroneous results and performance issues when querying or analyzing data. Fortunately, SQL provides a powerful set of tools for identifying and removing duplicate values from a table. In this comprehensive guide, we will explore various techniques and best practices for deduplicating your data using SQL.

How to remove duplicate values from a table?

Before diving into the intricacies of removing duplicate values, let's first understand why it's essential to address this issue:

Data Accuracy: Duplicate records can lead to inaccuracies in your data analysis, as each duplicated entry may be counted multiple times, skewing your results.

Storage Efficiency: Duplicate values consume unnecessary storage space, which can be a concern in large databases. Removing duplicates can help reduce storage costs.

Improved Query Performance: Queries on tables with duplicate values can be slower and less efficient. By removing duplicates, you can optimize query performance.

Data Integrity: Duplicate records can violate data integrity constraints and lead to inconsistencies in your database.

Now that we understand the importance of removing duplicate values, let's explore SQL techniques to achieve this.

Identifying Duplicate Values

Before you can remove duplicate values, you need to identify them. SQL provides several ways to do this:


The DISTINCT keyword in a SELECT statement allows you to retrieve unique values from a column or a set of columns. This is a straightforward way to identify duplicates.

SELECT DISTINCT column1, column2
FROM your_table;

However, this method only identifies duplicates if all the columns specified in the SELECT statement have the same values. If you want to identify duplicates based on a single column, you'll need a different approach.

2. Using GROUP BY and HAVING

The GROUP BY clause, in combination with the HAVING clause, is a powerful way to identify and count duplicate values based on one or more columns. You can use the COUNT function to determine how many times each set of values appears.

SELECT column1, column2, COUNT(*)
FROM your_table
GROUP BY column1, column2

This query will return the duplicate values along with their counts. If COUNT(*) is greater than 1, it means the combination of values in column1 and column2 is duplicated.

3. Using Common Table Expressions (CTEs)

Common Table Expressions (CTEs) are also a way to create temporary result sets within a query. They are particularly useful for identifying and working with duplicate values.

WITH Duplicates AS (
    SELECT column1, column2, COUNT(*) AS count
    FROM your_table
    GROUP BY column1, column2
    HAVING COUNT(*) > 1
SELECT * FROM Duplicates;

By using a CTE, you can modularize your code and make it more readable.

4. Self-Joins

Self-joins are another technique for identifying duplicates in a table. You can join a table with itself based on the columns you suspect contain duplicates.

FROM your_table a
JOIN your_table b
    ON a.column1 = b.column1
    AND a.column2 = b.column2
    AND <>;

In this query, we compare each row with all other rows in the same table based on column1 and column2. The condition <> ensures that we don't match a row with itself.

Removing Duplicate Values

Once you've identified the duplicate values in your table, it's time to remove them. SQL offers several methods to accomplish this, for example you can use the DELETE command to remove data from your table. 

1. Using DELETE with Subqueries

You can use the DELETE statement in combination with subqueries to remove duplicate rows. The subquery identifies the rows to be deleted based on the criteria you specify.

DELETE FROM your_table
    SELECT MIN(id)
    FROM your_table
    GROUP BY column1, column2
    HAVING COUNT(*) > 1

In this example, we keep the row with the lowest id value within each set of duplicate values and delete the rest.

2. Using Common Table Expressions (CTEs)

CTEs can also be helpful for deleting duplicate values. You first identify the duplicates in a CTE and then use that information to delete them from the main table.

WITH Duplicates AS (
    SELECT column1, column2, MIN(id) AS min_id
    FROM your_table
    GROUP BY column1, column2
    HAVING COUNT(*) > 1
DELETE FROM your_table
WHERE (column1, column2, id) NOT IN (
    SELECT column1, column2, min_id
    FROM Duplicates

This query removes duplicates by comparing each row's (column1, column2, id) with the minimum (column1, column2, min_id) within each set of duplicates.

3. Using Temporary Tables

Another approach is to use temporary tables to store the rows you want to keep and then truncate or recreate the original table.

CREATE TABLE temp_table AS
FROM your_table;

-- Truncate or drop the original table
TRUNCATE your_table;

-- Copy the deduplicated data back to the original table
INSERT INTO your_table
SELECT * FROM temp_table;

This method can be useful when dealing with complex relationships or constraints in your database.

4. Using the ROW_NUMBER() Window Function

The ROW_NUMBER() window function can be used to assign a unique row number to each row within a partition of the data. You can leverage this function to keep only one instance of each duplicate based on your desired criteria.

WITH RankedRows AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) AS rn
    FROM your_table
DELETE FROM RankedRows WHERE rn > 1;

In this query, we assign a row number to each row within each set of duplicates based on the column1 and column2 columns, and then delete rows with a row number greater than 1.

Best Practices for Deduplication

While the methods described above are effective for removing duplicate values, here are some best practices to keep in mind:

1. Backup Your Data

Before performing any deduplication operation, make sure to back up your data. Mistakes can happen, and having a backup ensures you can recover in case something goes wrong.

2. Choose the Right Method

Select the deduplication method that best suits your specific use case and database structure. Consider factors like table size, performance, and data integrity.

3. Test on a Subset

If you're dealing with a large dataset, it's a good idea to test your deduplication process on a subset of the data first. This allows you to identify and address any issues before applying it to the entire dataset.

4. Monitor Performance

Deduplication can be resource-intensive, especially on large tables. Monitor the performance of your database during the deduplication process to ensure it doesn't impact other operations.

5. Update Indexes and Statistics

After deduplication, update indexes and statistics on your table to ensure the database optimizer can make efficient query plans.

Quiz Time

Now, let's see how much you have learned in this article, if you can answer all question correct then you are good to go but if you can't, better read the article one more time, I Am sure you will learn better. 

1. What SQL keyword is commonly used to remove duplicate rows from a table?


2. Which SQL clause is used in combination with the DELETE statement to remove duplicates based on a specific column?


3. What does the following SQL statement do: DELETE FROM customers WHERE customer_id NOT IN (SELECT MAX(customer_id) FROM customers GROUP BY email)?

A) Deletes all customers from the "customers" table.
B) Deletes customers with the highest "customer_id" within each email group.
C) Deletes customers with the lowest "customer_id" within each email group.
D) Deletes customers with NULL email addresses.


That's all about how to remove duplicate values from a table in SQL. Removing duplicate values from a table is a common and essential task in SQL data management. SQL provides various techniques to identify and eliminate duplicates, allowing you to maintain data accuracy, improve storage efficiency, and enhance query performance. 

By following best practices and choosing the right method for your specific scenario, you can keep your database clean and reliable, ensuring the integrity of your data for meaningful analysis and decision-making. 

Deduplication is just one of the many powerful capabilities that SQL offers to data professionals, making it a valuable skill for anyone working with data.

Other SQL articles you may like:

Thank you so much for your time and reading the article completely.

No comments:

Post a Comment