How To Remove Duplicates In SQL?

Use `DISTINCT` to return unique rows

Use `GROUP BY` to group identical rows and keep one result

Use `ROW_NUMBER()` with a CTE or subquery to identify duplicates and delete extra rows

Use `RANK()` or `DENSE_RANK()` when duplicates need to be handled by priority rules

Use `DELETE` with a self-join to remove repeated rows

Use `EXISTS` or `NOT EXISTS` to keep only the first matching row

Use a temporary table to store unique rows, then replace the original table

Use `UNION` instead of `UNION ALL` to eliminate duplicate result rows

Add a `UNIQUE` constraint or index to prevent future duplicates

Use `SELECT DISTINCT ON` in databases that support it, such as PostgreSQL

Suggested for You

Trending Today