Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How to retrieve duplicate rows in a table?
How to delete the duplicate entries in a table?

Answer Posted / skybeaver

/* selecting duplicate rows in a table */
select col1, col2, ..., colN, count(*)
from TableName
group by col1, col2, ..., colN
having count(*) > 1

/* deleting duplicate rows from a table */
select col1, col2, ..., colN, count(*) as "Duplicates"
into #duplicates
from TableName
group by col1, col2, ..., colN
having count(*) > 1

delete TableName
from TableName t, #duplicates d
where t.col1 = d.col1 and
....
t.colN = d.colN

/* damn I'm good! */

Is This Answer Correct ?    11 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the different Topologies in which Replication can be configured?

1183


List down some advantages of sql stored procedure?

963


What are the different types of data sources in ssrs?

196


What is difference between order by and group by?

1006


How do you clear a log file?

1046


What is index, cluster index and nonclustered index?

995


What are indexes in ms sql server?

1057


What is a dbms wizard?

1020


What is the sql case statement used for?

1097


What to check if a User database is locked?

1069


What's new in sql management studio for sql server? : sql server management studio

1065


What is plan freezing?

968


How do I find the transaction log size in sql server?

961


where can you add custom error messages to sql server? : Sql server administration

1120


How do indexes help, types?

959