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 is conditional split?

1008


What are different types of collation sensitivity?

950


Explain foreign key in sql server?

980


do views contain data ?

920


Explain mixed authentication mode of sql server?

994


How to rebuild all indexes on a single table?

1001


Give an example of why you would want to denormalize a database

937


How to list all user defined functions in the current database?

988


What is bit data type?

989


What is the use of =,==,=== operators?

963


You have several tables, and they are joined together for querying. The tables contain both clustered indexes and non clustered indexes to optimize performance, how should you distribute the tables and their indexes onto different file groups?

953


How to set database to be read_only in ms sql server?

963


What is acid properties?

1030


How do I manually uninstall an instance of sql server 2016?

971


What is a fan-out query in SQL Azure?

121