Several ways to find duplicate records in SQL Server database

Source: Internet
Author: User

Http://www.hanyu123.cn/html/c61/6790.html

One, check a column (or more than one column) of the duplicate values. (You can only find the value of the duplicate record, cannot find the whole record information)

For example: Find Stuid,stuname duplicate records:

    1. Select stuid,stuname from stuinfo
    2. Group by Stuid,stuname
    3. Having(Count(*)) >1

Second, check a column has a record of duplicate values. (This method is to detect all duplicate records, if there are two records repeat, the detection of two)

For example: Find Stuid duplicate records:

    1. Select * from stuinfo
    2. where stuid in (
    3. Select stuid from stuinfo
    4. GROUP BY stuid
    5. Having(Count(*)) >1
    6. )

Third, check a column has a record of duplicate values. (Show only redundant records, which means two if there are three duplicates)

Prerequisite: There is a column that does not duplicate, this example is recno. For example: Find Stuid duplicate records:

    1. Select * from stuinfo s1
    2. Where recno not in (
    3. Select Max(recno) from stuinfo s2
    4. where s1. Stuid=S2. Stuid

Several ways to find duplicate records in SQL Server database

Related Article

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.