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

What is subquery in sql?

568


What is the syntax to execute the sys.dm_db_missing_index_details?

582


Explain can SSRS reports Cache results?

112


Can sub report data source be different from that of the parent report?

105


What is store procedure? How do they work?

579






Why are sql functions used?

622


Does the unique constraint create an index?

566


What is rolap and its advantage? : sql server analysis services, ssas

644


Explain the truncate command? : SQL Server Architecture

562


What is the process of normalization?

558


What guidelines should be followed to help minimize deadlocks?

502


What is pivot and unpivot?

640


In what version of sql server were synonyms released?

504


How do I view a script in sql server?

531


What are 3 ways to get a count of the number of records in a table?

609