adspace


Delete duplicate rows from a table without primary key by
using a single query
Table Employee
empname salary
A 200
B 300
A 200
C 400
D 500
D 500

Output should be

A 200
B 300
C 400
D 500

Answer Posted / sunil

set rowcount 1 -- set row count 1

delete a from Employee a
where (select count(*) from Employee e where e.empname =
a.empname) > 1

while @@rowcount > 0
begin
delete a from Employee a
where (select count(*) from Employee e where e.empname =
a.empname) > 1
end
set rowcount 0

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

explain different types of backups avaialabe in sql server? Given a particular scenario, how would you go about choosing a backup plan? : Sql server database administration

1134


What are the risks of storing a hibernate-managed object in a cache? How do you overcome the problems?

1200


What is a view in sql?

1043


What are the pros and cons of putting a scalar function in a queries select list or in the where clause?

1300


Why and when do stored procedure recompile?

1064


List out the different types of locks available in sql server?

1051


When should you use an instead of trigger?

1054


What are the different subsets of sql?

1654


Why we use the openxml clause?

1080


You have a stored procedure, which execute a lengthy batch job. This stored procedure is called from a trigger you do not want to slow the data entry process you do not want trigger to wait for this batch job to finish before it completes itself what you can do to speed up the process?

1215


How do I start sql server 2017?

1016


What are the different types of subquery?

1235


what is spatial nonclustered index

1060


What are the kinds of subquery?

1109


What is self contained sub query?

1141