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                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
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
how to delete duplicate rows from table in sql server
 Question Submitted By :: K L Baiju
I also faced this Question!!     Rank Answer Posted By  
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 1
delete  tblname where columname  in (select columnname 
FROM  tblname  GROUP BY  columnname having count(columnname)
>=2)
 
Is This Answer Correct ?    15 Yes 20 No
Suresh S
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 2
We can delete duplicate rows from table by using distinct 
command.
 
Is This Answer Correct ?    5 Yes 22 No
Swapna
 
 
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 3
delete from my_table where my_primary_key in
  (select a.primary_key from my_table a, my_table b
     where not a.my_primary_key = b.my_primary_key
       and [insert restriction that makes the 2 rows the same]
   )
 
Is This Answer Correct ?    0 Yes 2 No
Gerry
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 4
this can help if you want to keep only different records

create table test
(
 id int,
 name varchar(20)
)

insert into test VALUES(1,'test')
insert into test VALUES(2,'test')
insert into test VALUES(2,'test')
insert into test VALUES(3,'test')
insert into test VALUES(4,'test')
insert into test VALUES(5,'test')
insert into test VALUES(6,'test')
insert into test VALUES(7,'test')
insert into test VALUES(7,'test')
insert into test VALUES(7,'test')



select * from test order by 1

while @@rowcount != 0
begin
delete top (1) test where id  in 
(
    select id 
    FROM  test  
	GROUP BY  id having count(id)>1
)
end

select * from test order by 1


-- At the end you will have only differents IDS
 
Is This Answer Correct ?    4 Yes 2 No
Victor
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 5
CREATE TABLE dbo.duplicateTest      ------Deleting
Duplicates with same id 
( 
[ID] [int] , 
[FirstName] [varchar](25), 
[LastName] [varchar](25)  
) ON [PRIMARY] 

INSERT INTO dbo.duplicateTest VALUES(1, 'Bob','Smith') 
INSERT INTO dbo.duplicateTest VALUES(2, 'Dave','Jones') 
INSERT INTO dbo.duplicateTest VALUES(3, 'Karen','White') 
INSERT INTO dbo.duplicateTest VALUES(1, 'Bob','Smith') 



SELECT * FROM dbo.duplicateTest 

SET ROWCOUNT 1 
DELETE FROM dbo.duplicateTest WHERE ID = 1 
SET ROWCOUNT 0 

SELECT * FROM dbo.duplicateTest 

Drop table dbo.duplicatetest
 
Is This Answer Correct ?    3 Yes 1 No
Onlymoid
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 6
;with empctc(empid,ename,sal,deptid,ranking)
as
(Select empid,ename,sal,deptid,ranking=Dense_rank() over (
partition by empid,ename,sal,deptid order by NEWID() asc) 
from emp
)
delete * from empctc where ranking>1
 
Is This Answer Correct ?    3 Yes 1 No
Smitha
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 7
while @@rowcount != 0
begin
delete top (1) test where columnname  in 
(
    select columnname
    FROM  tablename 
	GROUP BY  columnname having count(*)>1
)
end
 
Is This Answer Correct ?    1 Yes 1 No
Debasish
 
  Re: how to delete duplicate rows from table in sql server
Answer
# 8
delete from table_name where column_name='value' and rowid 
not in (select max(rowid) from table_name where 
column_name='value');

e.g. create table duplicate (name varchar(15), rollno number
(10));

insert into duplicate (name,rollno) values ('mkumar',2);
insert into duplicate (name,rollno) values ('mkumar',2);

delete from duplicate where name='mkumar' and rowid not in 
(select max(rowid) from duplicate where name='mkumar');
 
Is This Answer Correct ?    1 Yes 0 No
Manjeet Kumar
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
What are defaults? Is there a column to which a default can't be bound?  2
Questions regarding Raiseerror?  1
What are the types of indexes available with SQL Server?  2
What is a transaction and what are ACID properties? IBM4
What is deadlock and how to avoid the deadlocks. Wipro3
how to write the query to select the rows are in the order of either 1,3,5,7... or 2,4,6,8,... ADP5
How do you use DBCC statements to monitor various aspects of a SQL server installation?  1
What is the difference between a local and a global variable?  3
What is an Index? Yardi-Software3
any one can explain Self mapping Delegation Remote Credentials  1
write the query for taking database restore in sql?  2
What are the authentication modes in SQL Server?  2
How do you simulate a deadlock for testing purposes  1
What is the use of DBCC commands?  1
how to find nth highest salary NexGen30
could u plz explain about joins and views?  2
i want only duplicates rows from coloumn ex. emp_id(colomn name)1,1,2,3,3,4,5,5. so i want only duplicates no. iFlex2
What is the Query of getting last 10 transaction Reports (like insert, update, Delete Data from Tabele) ? Wipro4
What is the difference between a Application Server and a Database Oracle2
1.can we set the more than 1 primary keys for a table? 2.please give me the difference between Cluster Index and non-Clustered Index 3.can we use query like this "Select * from Table1,Table2;"  6
 
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