How to retrieve duplicate rows in a table?
How to delete the duplicate entries in a table?

Answers were Sorted based on User's Feedback



How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?..

Answer / mon

DELETE FROM MyTable
LEFT OUTER JOIN (
SELECT MIN(RowId) as RowId, Col1, Col2, Col3
FROM MyTable
GROUP BY Col1, Col2, Col3
) as KeepRows ON
MyTable.RowId = KeepRows.RowId
WHERE
KeepRows.RowId IS NULL

Is This Answer Correct ?    0 Yes 0 No

How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?..

Answer / arun kumar k s

drop table #TEMP select distinct * into #TEMP from
TABLE_NAME delete from TABLE_NAME insert into TABLE_NAME
select * from #TEMP

Is This Answer Correct ?    0 Yes 3 No

How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?..

Answer / gaurav jain

begin
select distinct * into #one from four where id in (select
id from four group by id
having count(*)>1)
delete from four where id in (select id from four group by
id having count(*)>1)
insert into four select * from #one
end

Is This Answer Correct ?    0 Yes 3 No

How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?..

Answer / anoop rajan

Tbale emp had some duplicate entries and i wanted to retain
the first of all duplicates, the others could be deleted as
follows . Please give your comments if this is the most
optimum way :

delete from emp where rowid in
(select rowid from emp o where rowid !=
(select min(rowid) from emp i where i.empno=o.empno));

Is This Answer Correct ?    0 Yes 3 No

How to retrieve duplicate rows in a table? How to delete the duplicate entries in a table?..

Answer / pawan k. dubey

delete from employee
where Emp_id not in (select min(Emp_id) from employee
group by Emp_Name)

Is This Answer Correct ?    2 Yes 12 No

Post New Answer

More SQL Server Interview Questions

Explain log shipping and mention its advantages.

0 Answers  


How to assign new column names in a view?

0 Answers  


Do you know the capabilities of cursors?

0 Answers  


what is call by value & call by reference ?

2 Answers   Cap Gemini,


How to change the system date and time from SQL Plus Terminal ?

0 Answers   MCN Solutions,






What are the ways available in sql server to execute sql statements?

0 Answers  


What is the native system stored procedure to execute a command against all databases?

0 Answers  


Is it possible to have clustered index on separate drive from original table location?

0 Answers  


Explain cursor as data base object?

0 Answers  


What are different backups available in sql server?

0 Answers  


hi i gone though satyam interview. what is Acid Properties?

2 Answers   Satyam,


ow to bring suspect mode datbase online from scratch

0 Answers  


Categories