I want to delete the duplicate records from table and keep only one record.
Example: ID Name
1 Sinu, 2 Sinu, 3 Sinu, 4 Tinu, 5 Tinu
Output
1 Sinu, 4 Tinu
>
Having a unique ID makes this easy. By using a self referencing sub-query and the MIN aggregate function, you can determine the lowest ID for a given NAME. By deleting all but the lowest ID, you are left with a unique set of NAMEs.
delete from MyTable t1
where id <> ( select min(ID) from MyTable t2
where t2.Name = t1.Name )
Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Search and Browse the Expert Answer Center Search and browse more than 25,000 question and
answer pairs from more than 250 TechTarget industry experts.
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.