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 are the restrictions that views have to follow? : SQL Server Architecture

525


Thanks to some maintenance being done, the sql server on a failover cluster needs to be brought down. How do you bring the sql server down?

519


Can we use max in where clause?

505


How to remove duplicate rows from table?

587


What is difference between table aliases and column aliases? Do they affect performance?

503






How many categories of data types used by sql server?

584


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

515


Is it possible for a stored procedure to call itself or recursive stored procedure?

498


How to make a column nullable?

588


What is the command used to recompile the stored procedure at run time?

609


How to get a list of all tables with "sys.tables" view in ms sql server?

543


How to count rows with the count(*) function in ms sql server?

532


What do you mean by an execution plan? Why is it used? How would you view it?

523


Why I can not enter 0.001 second in date and time literals in ms sql server?

531


Explain what are the authentication modes in sql server?

494