SQL de-weight

Source: Internet
Author: User

SQL de-weight

Now there is a table T (Id,name), the ID is the primary key, name can be duplicated, and now you want to delete the duplicate data, preserving the data with the smallest ID. Please write out SQL.

Table: T

ID Name

1 sheets of three

2 sheets of three

3 John Doe

4 John Doe

5 John Doe

Analysis:

First, by name grouping, select each group ID minimum record. All data except those records is then deleted.

1:select min (id) id,name from t Groud by name.

Focus: Min (), groud by, not exists ()

Full sql:

Delete  from where  not exists Select *  from Select min  from Group  by where a.id=b.id)

SQL de-weight

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.