Top 5 Ways
to
Remove
Duplicate Values in SQL
Kishan Soni
1. ROW_NUMBER() with CTE
use ROW_NUMBER() to assign a unique number to each record within
the same group (name, age, grade).
The first occurrence of each group gets ROW_NUMBER() = 1, and
duplicates get ROW_NUMBER() > 1.
A CTE (Common Table Expression) stores this result temporarily.
Finally, delete all records where ROW_NUMBER() > 1, keeping only the
first occurrence.
-> This method is fast, scalable, and works well for large datasets!
Kishan Soni
2. Delete Duplicates Using SELF-JOIN
Joins the table with itself to find duplicate records.
Matches records with the same name, age, grade.
Deletes records where id > MIN(id), keeping only the first occurrence.
Kishan Soni
3. DELETE with EXISTS
Checks for duplicates using a correlated subquery.
If a duplicate exists, the outer query deletes the extra record (id >
MIN(id)).
More optimized than a JOIN in some databases because it stops
searching once a match is found.
Kishan Soni
4. Create Backup Table & TRUNCATE
Creates a backup table with only unique records (MIN(id)).
Truncates the original table (fastest way to remove all data)
.
Restores unique records from the backup table.
Drops the temporary backup table after restoration.
Kishan Soni
5. NOT IN with MIN(id) Method
Finds the smallest id (MIN(id)) for each duplicate group (item,
order_date).
Deletes all other duplicate records while keeping the first occurrence.
Simple and effective for smaller datasets.
⚠ Caution: NOT IN can be slower on large datasets. Consider using
JOIN or EXISTS for better performance.
Kishan Soni
Summary & Best Method
1. ROW_NUMBER() with CTE – Uses window functions for precise deletion.
2. Self-Join Approach – Simple & universal, works in all databases.
3. DELETE with EXISTS – Optimized for performance, stops searching after
finding a match.
4. Backup Table & TRUNCATE – Ensures data safety before deletion.
5. NOT IN with MIN(id) – Straightforward but less efficient on large datasets.
🔹 Best Method
ROW_NUMBER() with CTE
→ Works efficiently on any database size!
→ Optimized execution with window functions.
Kishan Soni
If you
find this
helpful, please like
and share it with
your friends
Kishan Soni