Multiple ways to delete duplicate records in SQL

Source: Internet
Author: User

After learning about SQL for a while, I found a lot of duplicate records in the table I built to test (not indexed). Later, we summarize some ways to delete duplicate records, in Oracle, you can delete duplicate records through the unique ROWID, and you can build temporary tables to implement ... This only refers to a few of the simple and practical methods, I hope to share with you (take the table employee as an example).

Sql> DESC Employee

Name Null? Type

----------------------------------------- -------- ------------------

emp_id

Number (10)

Emp_name

VARCHAR2 salary Number (10,2) can query for duplicate records through the following statement:

Sql> SELECT * from employee; emp_id Emp_name SALARY

---------- ---------------------------------------- ----------

1 Sunshine 10000 1 Sunshine 10000 2 Semon 20000 2 SE Mon 20000 3 xyz 30000 2 Semon 20000

Sql> SELECT DISTINCT * from employee;

emp_id emp_namesalary

---------- ---------------------------------------- ----------

1 Sunshine 10000

2 Semon 20000

3 xyz 30000

Sql> SELECT * FROM Employee GROUP BY Emp_id,emp_name,salary has count (*) >1

emp_id Emp_name SALARY

--------- ---------------------------------------- ----------

1 Sunshine 10000

2 Semon 20000

Sql> SELECT * FROM Employee E1 where rowID to (select Max (ROWID) from Employe E2

where e1.emp_id=e2.emp_id and E1.emp_name=e2.emp_name and e1.salary=e2.salary);

emp_id Emp_name SALARY

---------- ---------------------------------------- ----------

1 Sunshine 10000

3 xyz 30000

2 Semon 20000

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.