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
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 )
For More Information
- Dozens more answers to tough SQL questions from Jason Law are available here.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- 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.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.