How To Delete Repeated Rows In Sql

6 min read

Introduction

Learning how to delete repeated rows in SQL is essential for maintaining clean and efficient databases; this guide explains step‑by‑step methods, best practices, and common pitfalls to ensure your data remains accurate and reliable Small thing, real impact..

Understanding Duplicate Data

What Constitutes a Repeated Row?

A repeated row, also called a duplicate record, occurs when two or more rows share identical values across one or more columns that define uniqueness (e.g., primary key or a composite key). SQL does not automatically enforce uniqueness unless a constraint (PRIMARY KEY, UNIQUE) is defined, so duplicates can slip in during data entry, import processes, or manual updates Still holds up..

Counterintuitive, but true That's the part that actually makes a difference..

Why Remove Duplicates?

  • Data Integrity: Duplicate rows can skew aggregations, reports, and analytics.
  • Storage Efficiency: Eliminating excess rows reduces storage consumption and improves query performance.
  • Business Logic: Many applications rely on a single “correct” version of each entity; duplicates violate that logic.

Identifying Duplicate Rows

Before deleting, you must locate the duplicates. Below are common techniques:

  • Self‑Join Method

    SELECT a.*
    FROM your_table a
    JOIN your_table b
      ON a.column1 = b.column1
     AND a.column2 = b.column2
     AND a.id <> b.id;   -- id is a unique identifier
    

    This query returns all rows that have at least one partner with matching values but a different primary key.

  • GROUP BY with HAVING

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

    This groups rows by the columns that define duplication and filters groups that appear more than once.

  • Window Functions

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

    ROW_NUMBER() assigns a sequential number within each duplicate group; rows with a number greater than 1 are the extra copies.

Deleting Duplicate Rows: Step‑by‑Step

Method 1 – DELETE with ROW_NUMBER()

This is the most straightforward and safest approach when you need to keep a single “primary” row (often the one with the smallest id).

  1. Create a CTE (Common Table Expression) that numbers the rows

    WITH cte AS (
        SELECT *,
               ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) AS rn
        FROM your_table
    )
    
  2. Delete rows where the row number exceeds 1

    DELETE FROM your_table
    WHERE id IN (
        SELECT id FROM cte WHERE rn > 1
    );
    

    Why it works: The CTE marks every duplicate with a sequential number; only the rows beyond the first are removed.

  3. Wrap in a Transaction (optional but recommended)

    BEGIN TRANSACTION;
    -- CTE and DELETE statements here
    COMMIT;
    

    This ensures you can roll back if something goes wrong And that's really what it comes down to..

Method 2 – DELETE Using a Subquery with GROUP BY

Every time you want to keep the row with the highest id (or any other criteria), you can use a subquery that identifies the maximum id per duplicate group Not complicated — just consistent..

DELETE FROM your_table
WHERE id NOT IN (
    SELECT MAX(id)
    FROM your_table
    GROUP BY column1, column2
);
  • Explanation: The subquery returns the highest id for each unique combination of duplicate columns. The outer DELETE removes any row whose id is not among those maxima, effectively keeping the “latest” row and discarding the rest.

Method 3 – Temporary Table Approach

If you need more control (e.Plus, g. , archiving duplicates before deletion), you can move data to a temporary table, delete, then re‑insert what you want to keep.

-- 1. Create a temp table to store rows to keep
CREATE TABLE #KeepRows (
    id INT PRIMARY KEY,
    column1 VARCHAR(50),
    column2 VARCHAR(50),
    -- other columns as needed
);

-- 2. Insert the row with the smallest id per duplicate group
INSERT INTO #KeepRows (id, column1, column2)
SELECT MIN(id), column1, column2
FROM your_table
GROUP BY column1, column2;

-- 3. Delete all rows from the original table
DELETE FROM your_table;

-- 4. Re‑insert the rows you decided to keep
INSERT INTO your_table (id, column1, column2, ...)
SELECT * FROM #KeepRows;

-- 5. Drop the temporary table
DROP TABLE #KeepRows;

This method is handy when you need to audit the rows you are deleting or when you must preserve specific rows based on business rules Worth knowing..

Preventing Future Duplicates

Enforce Constraints

  • PRIMARY KEY: Guarantees each row is unique by definition.
  • UNIQUE Constraint: Allows multiple nulls but prevents duplicate non‑null values in the specified column(s).
ALTER TABLE your_table
ADD CONSTRAINT uq_unique_columns UNIQUE (column1, column2);

Use UPSERT (MERGE) for Inserts

When inserting data from external sources, use an UPSERT to avoid creating duplicates:

MERGE INTO your_table AS target
USING (SELECT column1, column2, column3 FROM source) AS src
ON target.column1 = src.column1 AND target.column2 = src.column2
WHEN NOT MATCHED THEN
    INSERT (column1, column2, column3) VALUES (src.column1, src.column2, src.column3);

Batch Validation

Before loading large CSV or Excel files, validate them with a script that checks for duplicate keys. This prevents bulk insertion of repeated rows.

Common Mistakes and How to Avoid Them

  • Deleting All Rows Instead of Just Duplicates
    Mistake: Using DELETE FROM your_table WHERE 1=1 or omitting the partition clause.
    Fix: Always filter with a condition that targets only the duplicate rows (e.g., rn > 1) Simple, but easy to overlook..

  • Forgot to Commit/Rollback
    Mistake: Running a massive DELETE without a transaction, which can lock the table and cause timeouts.
    Fix: Wrap the operation in BEGIN TRANSACTION … COMMIT (or ROLLBACK on error) The details matter here..

  • Using DELETE Without a WHERE Clause
    Mistake: Accidentally removing the entire table.
    Fix: Double‑check the WHERE clause; test the SELECT first to verify the rows to be deleted.

  • Relying Solely on Primary Key for Uniqueness
    Mistake: Assuming a non‑key column combination is unique when it isn’t.
    Fix: Identify the exact columns that define a duplicate and partition by those in window functions.

FAQ

Q1: Can I delete duplicates without losing the “best” row?
A: Yes. Choose a criterion (e.g., highest id, latest timestamp) and keep rows that satisfy it, then delete the rest.

Q2: Will deleting duplicates affect foreign key relationships?
A: If other tables reference the duplicated rows via foreign keys, you must either cascade the delete or first update those references.

Q3: Is there a performance impact?
A: Deleting duplicates can be resource‑intensive on large tables. Using indexes on the columns in the PARTITION BY clause, and processing in batches, mitigates the impact.

Q4: What if I need to keep more than one duplicate row?
A: Adjust the row‑numbering logic. As an example, keep the top 2 rows by using ROW_NUMBER() … ORDER BY id and delete where rn > 2 Still holds up..

Q5: How can I verify that duplicates are truly gone?
A: Run a GROUP BY … HAVING COUNT(*) > 1 query again after deletion; it should return zero rows.

Conclusion

Deleting repeated rows in SQL is a fundamental skill for any data professional. Complement these actions with preventive measures—primary keys, unique constraints, and UPSERT logic—to keep your database clean long term. Think about it: avoid common pitfalls by testing your DELETE statements, using transactions, and ensuring you target only the unwanted rows. By first identifying duplicates with self‑joins, GROUP BY, or window functions, then applying a safe deletion method such as DELETE with ROW_NUMBER(), you preserve data integrity while improving performance. With these practices, your SQL environment will remain efficient, reliable, and ready for accurate analytics.

Fresh Out

Newly Published

Related Territory

You May Enjoy These

Thank you for reading about How To Delete Repeated Rows In Sql. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home