SQL query to remove duplicate rows from one table?

Suppose there is an employee table containing column names, identifier and salary having 2 or more two rows with the same values ​​in all three rows ... then how to write a query to remove duplicate rows ..

+3
source share
5 answers

Here is a good way if you are using Sql Server

with duplicates as
(select * ,ROW_NUMBER() over(

      partition by id,name, salary
      order by id,name, salary) rownum
from Person)
delete from duplicates where rownum > 1
+8
source

The intended identifier is the primary key:

delete P
from Person P right outer join
(
   select name, min(id) as id
   from Person
   group by name
) unique_people
on P.id = unique_people.id
where P.id is NULL
+3
source

u ...

table_name, id = @id name= @name

0

Insert individual rows from the source table into the new temporary table. Delete the data from the original duplicate table, then insert the individual rows from the temporary table into the original table.

select distinct * into temp From Emp;
delete from Emp;
insert into Emp(name,ID,salary) as select * from temp; 
drop table temp;
0
source
DELETE TOP(1) FROM tablename WHERE columemane='name'

Suppose I have two repeating rows in a student table :

name | number | surname

Manoah | 1125256 | Sharma

Manoah | 1125256 | Sharma

I want to delete one using the following query

DELETE TOP(1) FROM student WHERE name='Manoj'
-1
source

Source: https://habr.com/ru/post/1781675/


All Articles