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
What is conditional split?
What are different types of collation sensitivity?
Explain foreign key in sql server?
do views contain data ?
Explain mixed authentication mode of sql server?
How to rebuild all indexes on a single table?
Give an example of why you would want to denormalize a database
How to list all user defined functions in the current database?
What is bit data type?
What is the use of =,==,=== operators?
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?
How to set database to be read_only in ms sql server?
What is acid properties?
How do I manually uninstall an instance of sql server 2016?
What is a fan-out query in SQL Azure?