ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Databases  >>  SQL Server
 
 


 

 
 Oracle interview questions  Oracle Interview Questions
 SQL Server interview questions  SQL Server Interview Questions
 MS Access interview questions  MS Access Interview Questions
 MySQL interview questions  MySQL Interview Questions
 Postgre interview questions  Postgre Interview Questions
 Sybase interview questions  Sybase Interview Questions
 DB Architecture interview questions  DB Architecture Interview Questions
 DB Administration interview questions  DB Administration Interview Questions
 DB Development interview questions  DB Development Interview Questions
 SQL PLSQL interview questions  SQL PLSQL Interview Questions
 Databases AllOther interview questions  Databases AllOther Interview Questions
Question
Write a query to delete duplicate records in SQL SERVER
 Question Submitted By :: Durga Prasad
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 1
DELETE FROM A
WHERE     (id NOT IN
                          (SELECT     MAX(ID)
                            FROM          A
                            GROUP BY name))

suppose i have table called A. A has two coloumn id
(identity) as int and Name as nvarchar. I have data like 
this.

id Name
1  C
2  D
3  C
4  E
5  D
6  D
 
Is This Answer Correct ?    22 Yes 14 No
Deepak Raheja
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 2
There is a table like this: tablename: example


Name                        Age
chandran                     23
ranjith                      24
chandran                     23

In this table the name:chandran and age:23 are the 
duplicate records  .so we need to delete this using this 
sql statements


delete from example group by name,age having count>1
 
Is This Answer Correct ?    16 Yes 28 No
Chandran
 
 
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 3
Mr. Chandran, I tried your query but the system is throwing 
error message  as 'Incorrect syntax near the 
keyword 'group''.
 
Is This Answer Correct ?    11 Yes 6 No
Pavan Kumar
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 4
SELECT distinct column_names INTO temp_table FROM main_table
drop table main_table
sp_rename temp_table,main_table
 
Is This Answer Correct ?    6 Yes 3 No
Abc
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 5
Hi friends, please just try out this. This works fine for me.

We have lot of methods to do this. But using temp table,
drop the original table,retain the temp as orinial is not a
good pratice.

When u have large no of data it will affect ur performance.


DELETE FROM employee WHERE((SELECT eid,COUNT(eid) FROM
employee GROUP BY eid) > 1)
 
Is This Answer Correct ?    7 Yes 8 No
Skumar
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 6
Name                        Age
chandran                     23
ranjith                      24
chandran                     23

To delete one of the duplicate records use following query 

(For sql server 2000)

Set rowcount 1
delete from [tableName] order by name 
set rowcount 0
--Write a cursor to delete multiple duplicate records 
Or (In sql server 2005)

;with DelDup as (select row_number() over (partition by 
sname order by sname) as RONO ,sname from [TableName]) 
Delete from DelDup where RONO > 1
 
Is This Answer Correct ?    10 Yes 1 No
Sagun Sawant
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 7
Table Name: Example

Name                        Age
chandran                     23
ranjith                      24
chandran                     23

To delete one of the duplicate records use following query 


delete from example where age in(select age from example 
group by age having count>1)
 
Is This Answer Correct ?    3 Yes 11 No
Chandran.s
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 8
This will help u a lot bcz dynamically it will delete 
duplicates



create table tbl1    (col1 int)





insert into tbl1 values(1)
insert into tbl1 values(1)
insert into tbl1 values(1)
insert into tbl1 values(2)
insert into tbl1 values(2)
insert into tbl1 values(2)
insert into tbl1 values(2)
insert into tbl1 values(3)
insert into tbl1 values(3)


select * from tb1


set rowcount 1
select 'start'

while @@rowcount > 0 
delete a from tbl1 a where (select count(*) from tbl1 b 
where a.col1 = b.col1)>1
set rowcount 0
select * from tbl1
set nocount off
 
Is This Answer Correct ?    4 Yes 5 No
Rama Krishna
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 9
delete from tab_nam where
 
Is This Answer Correct ?    1 Yes 12 No
Anil
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 10
Create table info(comp_id int identity(101,1),comp_name 
varchar(50))
insert into info values('Progressive Ltd.')
insert into info values('Progressive Ltd.')
insert into info values('EliResearch')
insert into info values('Patni')
insert into info values('Accenture')
insert into info values('Accenture')
select * from info

DELETE FROM info 
WHERE comp_name IN
(SELECT comp_name FROM info 
GROUP BY comp_name HAVING COUNT(comp_name) > 1)
 
Is This Answer Correct ?    2 Yes 13 No
Sonia
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 11
WITH INTER_TABLE(ROW, COL1) AS( 
select row_number() over (partition by 
COL1 order by COL1) as ROW ,COL1 from [TABLE_NAME]
)
Delete from INTER_TABLE where ROW > 1
 
Is This Answer Correct ?    11 Yes 2 No
Vijay
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 12
your table look like this and want to delete duplicate 
record

chandran                     23
ranjith                      24
chandran                     23


delete top(1) from tablename where name='chandran' and 
age=23
 
Is This Answer Correct ?    2 Yes 14 No
Dharmesh
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 13
Delete From Tablename where(ID Not in (Select max(ID) from 
Tablename Group by name))

Tablename :Friend

ID    Name   Age    city
101   vinod    22    Gwalior
102   Pritesh  23    Gwalior
102   Pritesh  23    Gwalior
103   Arvind   24    Gwalior


Here Id-102 is repeated so friend if u want to delete this 
duplicate raw  Try Above code in Sql-sever
 
Is This Answer Correct ?    2 Yes 8 No
Vinod Singh Kushwah
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 14
This query delete duplicate records(but not delete both
duplicate)
set rowcount 1
delete yourtable
from yourtable a
where (select count(*) from yourtable b where b.name=a.name
and b.age=a.age)>1
while @@rowcount >0
delete yourtable
from yourtable a
where(select count(*) from yourtable b b.name=a.name and
b.age=a.age)>1
set rowcount 0
 
Is This Answer Correct ?    1 Yes 8 No
Senthilsjc
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 15
I have The Same Problem And I Have Done Woth This 


DECLARE @empid int, @empname varchar(50),@Cnt int

DECLARE duplicate_cursor CURSOR FOR

-- select all columns in table bit you must have an count column
select empid,empname, count(*) Cnt
from tbl_Temp
group by empid, empname
Having count(*) > 1

OPEN duplicate_cursor

FETCH NEXT FROM duplicate_cursor
INTO @empid, @empname,@Cnt

WHILE @@FETCH_STATUS = 0
BEGIN

SET @Cnt = @Cnt - 1

SET ROWCOUNT @Cnt

DELETE tbl_Temp
WHERE @empid = empid AND @empname = empname 


FETCH NEXT FROM duplicate_cursor
INTO @empid, @empname
END

CLOSE duplicate_cursor
DEALLOCATE duplicate_cursor

-- dont forget to set rowcount to 0
SET ROWCOUNT 0
 
Is This Answer Correct ?    6 Yes 2 No
Sumit
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 16
select * from dbo.duplicatetest
set rowCount 1
delete from dbo.duplicatetest where iD=1;
set rowCount 0
 
Is This Answer Correct ?    0 Yes 5 No
Ashish Kumar
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 17
DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, 
DuplicateColumn2)
 
Is This Answer Correct ?    6 Yes 2 No
Anuj Dhingra
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 18
using the following query u can delete the duplicate records.
but it is available only on SQL Server 2005.

with mytable as                                     
(                                      
 select *, ROW_NUMBER() OVER(partition by id,myname order by
id desc) as RowNumber from test                          
)
delete from mytable where rownumber>1


"Have a Great Day"

Bala
 
Is This Answer Correct ?    2 Yes 0 No
Bala_it243
 
  Re: Write a query to delete duplicate records in SQL SERVER
Answer
# 19
I have a query that I use with success I have a table with 
telephone numbers in it and sometimes I have duplicate 
phone numbers in the table... here is how I see what they 
are and then remove them. 

===========================================

select telephone 
from Table_A
group by telephone having count(*) > 1

SET ROWCOUNT 1
SELECT NULL
WHILE @@rowcount > 0
DELETE step
FROM Table_A as step
INNER JOIN
(SELECT telephone
FROM Table_A
GROUP BY telephone HAVING count(*) > 1)
AS t ON t.telephone = step.telephone
SET ROWCOUNT 0

===========================================

This query will actually find and remove the duplicates 
from Table_A but will not remove both instances it will 
only remove one... leaving you with one good record... hope 
this helps someone.   : )
 
Is This Answer Correct ?    2 Yes 0 No
Ben Mccameron
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
MULTICAST DELEGATES IN C#.NET WITH REAL TIME EXAMPLE IBM1
how to replace double quotes by single quotes in sql server CAC2
plz send every query in sql server2000 Infosys1
What is the difference between Userdefined function and stored procedure? Explain and give the example also  4
how to connect sybase to sql server 2005?. ABC1
What is MSDE?  2
WHAT IS TRIGGERS IN SQL? AND WHAT IS THE BENIFIT?  5
wht is normalization?can u explain me in detail? TCS6
Which is better in performance - CONSTRAINT or TRIGGER over a column which restricts say an input of particular value in a column of a table? Accenture3
how to count datewise data in sqlserver IndusInd-Bank3
Wht is the difference between stored procedure and trigger TCS4
what is the out put of below queries? a. select * from Emp where null = null; b. select * from Emp where 1=1; Patni9
which one will take 1st priority in case of insert statement and select statement???  1
How do you measure the performance of a stored procedure? Infosys1
What is the default value of CHAR type? Bosch8
Rate yourself in .NET and SQL ? Cognizent1
What is the difference between windows authentication and sql server authentication HCL4
what are the joins,primary key,foriegn key, candidate key, super key and expain them? Polaris2
How do we rollback the table data in SQL Server  3
How to find the date and time of last updated table? Teledata4
 
For more SQL Server Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com