userimage
Moe

how to remove duplicate rows using SQL query in microsoft sql server.

how to remove duplicate rows using SQL query in Microsoft SQL server.

basically my requirement is i have a table with no constraint.

Now that table contains lots of duplicate data rows.

how can i remove all duplicate rows in one sort and add constraint in that table.

userimage

In sql we can use below:

to delete duplicates Firstly you need to find out a column which can be a primary key for that table.use that column in below query


delete from table e where rowid not in (select max(rowid) from table e1 where e.primarykeycolumn=e1.primarykeycolumn);

based on your requirement you can delete  latest record or old record by changing max,min in above query.


Answer is