SQL Delete duplicate records

Source: Internet
Author: User

Test data preparation

Give Test_user access to the Scott User EMP table

Grant Select  on  to Test_user;

Use Test_user to create an EMP table based on the Scott User's EMP table

Create Table  as Select *  from Scott.emp;

Insert the same data again

Insert  into Select *  from EMP;

Querying Current data

Select *  from Order  by ASC;

You can see that each record appears twice.

Delete duplicate records

1, delete redundant records in the table, duplicate records are based on a single field (EMPNO) to judge, leaving only the smallest ROWID records

Delete  fromEMPwhereEmpnoinch(SelectEmpno fromEmpGroup  byEmpno having Count(empno)> 1) androwID not inch(Select min(ROWID) fromEmpGroup  byEmpno having Count(empno)>1)

2, delete redundant records in the table, duplicate records are based on a number of fields (Empno, ename) to judge, leaving only the smallest records ROWID

Delete  fromEMP awhere(A.empno, A.ename)inch(SelectEmpno,ename fromEmpGroup  byEmpno,ename having Count(*)> 1) androwID not inch(Select min(ROWID) fromEmpGroup  byEmpno,ename having Count(*)>1)

SQL Delete duplicate records

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.