Find duplicates in SQL

前端 未结 2 1938
说谎
说谎 2020-12-16 18:29

I have a large table with the following data on users.

social security number
name
address

I want to find all possible duplicates in the ta

相关标签:
2条回答
  • 2020-12-16 19:06

    This will handle more than two records with duplicate ssn's:

    select count(*), name from table t1, (    
        select count(*) ssn_count, ssn 
        from table 
        group by ssn 
        having count(*) > 1
    ) t2
    where t1.ssn = t2.ssn
    group by t1.name
    having count(*) <> t2.ssn_count
    
    0 讨论(0)
  • 2020-12-16 19:21

    A grouping on SSN should do it

    SELECT
       ssn
    FROM
       Table t1
    GROUP BY
       ssn
    HAVING COUNT(*) > 1
    

    ..or if you have many rows per ssn and only want to find duplicate names)

    ...
    HAVING COUNT(DISTINCT name) > 1 
    

    Edit, oops, misunderstood

    SELECT
       ssn
    FROM
       Table t1
    GROUP BY
       ssn
    HAVING MIN(name) <> MAX(name)
    
    0 讨论(0)
提交回复
热议问题