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

How to define output parameters in stored procedures?

709


how you can deploy an ssrs report?

139


What is the difference between the 2 operating modes of database mirroring?

808


What are the types of resultset?

710


Define full outer join in sql server joins?

669






How to list all dsn entries on your local machine using odbc_data_source()?

754


Explain syntax for disabling triggers?

709


What is openxml in sql server?

878


What is difference between rownum and rowid?

669


How would you use user_constraints table in DB?

750


Can a stored procedure call itself or a recursive stored procedure? How many levels of sp nesting is possible?

795


Explain nested join?

740


What do you mean by a dependent functionality in a build?

832


What are three ways you can use an identity value inside a trigger? Why would you prefer one way over another?

746


Does windows server 2016 come with sql server?

677