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

How would you add a section to a table?

966


What is self contained multi valued query?

948


What are the disadvantages of primary key and foreign key in SQL?

1047


How to get a list of columns using the "sys.columns" view in ms sql server?

918


What types of Joins are possible with Sql Server?

1058


What does REVERT do in SQL Server 2005?

913


What are the default system databases in sql server 2000?

961


How do indexes help, types?

909


What protocol does sql server use?

881


How to see the event list of an existing trigger using sys.trigger_events?

1023


How do users use Report Builder with SQL Server data sources?

146


explain different levels of normalization? : Sql server database administration

928


What are ddl triggers and types of ddl trigger?

974


How to create indexed view?

985


If a user does not have permission to a table, but has permission to a view created on it, will he be able to view the data in table?

887