if 3 duplicate records in a table,i want to delete 2 duplicate
records by keeping 1 duplicate and 1 original as it is,how?

Answers were Sorted based on User's Feedback



if 3 duplicate records in a table,i want to delete 2 duplicate records by keeping 1 duplicate and ..

Answer / mohamed zunu

create table sampletbl(id int,name varchar(100))

insert into sampletbl values(1,'aa')
insert into sampletbl values(1,'aa')
insert into sampletbl values(1,'aa')


with cte as(
select ROW_NUMBER() over (partition by id order by id) as r_no,* from sampletbl)
delete from cte where r_no>1
select * from sampletbl

Is This Answer Correct ?    9 Yes 1 No

if 3 duplicate records in a table,i want to delete 2 duplicate records by keeping 1 duplicate and ..

Answer / siva raman

By using Rank function we can delete duplicate records in
tables.

Is This Answer Correct ?    6 Yes 3 No

if 3 duplicate records in a table,i want to delete 2 duplicate records by keeping 1 duplicate and ..

Answer / amol maske

DELETE FROM tablename WHERE ROWID NOT IN(
SELECT MIN(ROWID)FROM tablename GROUP BY columnname);

Is This Answer Correct ?    5 Yes 4 No

if 3 duplicate records in a table,i want to delete 2 duplicate records by keeping 1 duplicate and ..

Answer / dhananjay

The simplest way to eliminate the duplicate records is to
SELECT DISTINCT into a temporary table, truncate the
original table and SELECT the records back into the original
table. That query looks like this:

select distinct *
into #holding
from dup_authors

truncate table dup_authors

insert dup_authors
select *
from #holding

drop table #holding

Is This Answer Correct ?    0 Yes 0 No

if 3 duplicate records in a table,i want to delete 2 duplicate records by keeping 1 duplicate and ..

Answer / arun ashok

STEP 1 : Insert distinct value in the one new table.
STEP 2 : Delete the values from existing table.
STEP 3 : Again insert the values from new table to existing
table.

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More SQL Server Interview Questions

Explain sp_configure commands, set commands?

3 Answers  


Does a full backup include transaction log?

0 Answers  


what is the query and condition to delete datas in sql server.

2 Answers  


What is transaction server distributed transaction?

0 Answers  


what is the difference between Delete and Truncate

4 Answers   CarrizalSoft Technologies, Geo Research Centre,






Do you know what is raid and what are different types of raid configurations? : SQL Server Architecture

0 Answers  


What are the essential components of sql server service broker?

0 Answers  


how many non clustered index in sql server 2008,2010,2012

2 Answers   Accenture,


How to delete existing triggers using "drop trigger"?

0 Answers  


what is the maximum size of a row? : Sql server database administration

0 Answers  


sql server syntax to add "!" sign to "name" field of "employee" table in a manner that all names have the same lenght of 20 characters

1 Answers  


What is difference between materialized view and view?

0 Answers  


Categories