Write a query to delete duplicate records in SQL SERVER
Answers were Sorted based on User's Feedback
Answer / saurabh dixit
DECLARE @people TABLE
(
name VARCHAR(32),
age INT
)
insert into @people
select Name,age From People group by Name,Age having COUNT(name)>1
delete People from People pe
join @people p on pe.name=p.name and pe.age=p.age
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / prakash patel
delete from tbl_test where ID in
(select ID from tbl_test group by ID having count(ID) > 1)
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / vaishali
set rowcount 1
delete from stud
where (select count(*) from stud a where stud.roll_no =
a.roll_no) > 1
while @@rowcount<>0
delete from stud
where (select count(*) from stud a where stud.roll_no =
a.roll_no) > 1
set rowcount 0
| Is This Answer Correct ? | 3 Yes | 6 No |
Answer / shashank shrivastava
Table-admin
------------------
adminID adminName
1 admin
2 tester
3 admin
SQL:
---------------
DELETE admin.* FROM admin,
(SELECT count(adminName) as c, MAX(adminID) as m from
admin GROUP BY adminName HAVING c>1) as new_admin
WHERE admin.adminID=new_admin.m
| Is This Answer Correct ? | 1 Yes | 5 No |
Answer / senthilsjc
This query delete duplicate records(but not delete both
duplicate)
set rowcount 1
delete yourtable
from yourtable a
where (select count(*) from yourtable b where b.name=a.name
and b.age=a.age)>1
while @@rowcount >0
delete yourtable
from yourtable a
where(select count(*) from yourtable b b.name=a.name and
b.age=a.age)>1
set rowcount 0
| Is This Answer Correct ? | 6 Yes | 12 No |
Answer / ashish kumar
select * from dbo.duplicatetest
set rowCount 1
delete from dbo.duplicatetest where iD=1;
set rowCount 0
| Is This Answer Correct ? | 3 Yes | 10 No |
Answer / rajkamal
delete form emp
where (select distinct(ename)from emp)
| Is This Answer Correct ? | 3 Yes | 11 No |
Delete From Tablename where(ID Not in (Select max(ID) from
Tablename Group by name))
Tablename :Friend
ID Name Age city
101 vinod 22 Gwalior
102 Pritesh 23 Gwalior
102 Pritesh 23 Gwalior
103 Arvind 24 Gwalior
Here Id-102 is repeated so friend if u want to delete this
duplicate raw Try Above code in Sql-sever
| Is This Answer Correct ? | 4 Yes | 13 No |
Answer / anuj
You have Two option You can use keyword Distinct or Group By Both will help you to delete dublicate records
| Is This Answer Correct ? | 1 Yes | 11 No |
What is data source object?
How can you find out which stored procedures are recompiling?
How to restore performance issues and how to check?
How can I create a new template for import ? : sql server management studio
How can I track the changes or identify the latest insert-update-delete from a table?
What security features are available for stored procedure?
How do you find value of first column before inserting value into the second column in the same table for checking that second column must have different value than first column.
Explain about local stored procedure?
Where are sql server user names and passwords stored in sql server?
what is Constraint? How many types of constraints in SQL ?
38 Answers HCL, IBM, NIIT, Wipro,
How to list all schemas in a database?
What are the requirements on sql server network connections?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)