Finding duplicate values in a SQL table

后端 未结 30 3994
南旧
南旧 2020-11-21 13:18

It\'s easy to find duplicates with one field:

SELECT name, COUNT(email) 
FROM users
GROUP BY email
HAVING COUNT(email) > 1

So if we have

相关标签:
30条回答
  • 2020-11-21 13:41

    If you wish to see if there is any duplicate rows in your table, I used below Query:

    create table my_table(id int, name varchar(100), email varchar(100));
    
    insert into my_table values (1, 'shekh', 'shekh@rms.com');
    insert into my_table values (1, 'shekh', 'shekh@rms.com');
    insert into my_table values (2, 'Aman', 'aman@rms.com');
    insert into my_table values (3, 'Tom', 'tom@rms.com');
    insert into my_table values (4, 'Raj', 'raj@rms.com');
    
    
    Select COUNT(1) As Total_Rows from my_table 
    Select Count(1) As Distinct_Rows from ( Select Distinct * from my_table) abc 
    
    0 讨论(0)
  • 2020-11-21 13:41

    SELECT id, COUNT(id) FROM table1 GROUP BY id HAVING COUNT(id)>1;

    I think this will work properly to search repeated values in a particular column.

    0 讨论(0)
  • 2020-11-21 13:41

    This should also work, maybe give it try.

      Select * from Users a
                where EXISTS (Select * from Users b 
                    where (     a.name = b.name 
                            OR  a.email = b.email)
                         and a.ID != b.id)
    

    Especially good in your case If you search for duplicates who have some kind of prefix or general change like e.g. new domain in mail. then you can use replace() at these columns

    0 讨论(0)
  • 2020-11-21 13:44

    This selects/deletes all duplicate records except one record from each group of duplicates. So, the delete leaves all unique records + one record from each group of the duplicates.

    Select duplicates:

    SELECT *
    FROM table
    WHERE
        id NOT IN (
            SELECT MIN(id)
            FROM table
            GROUP BY column1, column2
    );
    

    Delete duplicates:

    DELETE FROM table
    WHERE
        id NOT IN (
            SELECT MIN(id)
            FROM table
            GROUP BY column1, column2
    );
    

    Be aware of larger amounts of records, it can cause performance problems.

    0 讨论(0)
  • 2020-11-21 13:44

    In case you work with Oracle, this way would be preferable:

    create table my_users(id number, name varchar2(100), email varchar2(100));
    
    insert into my_users values (1, 'John', 'asd@asd.com');
    insert into my_users values (2, 'Sam', 'asd@asd.com');
    insert into my_users values (3, 'Tom', 'asd@asd.com');
    insert into my_users values (4, 'Bob', 'bob@asd.com');
    insert into my_users values (5, 'Tom', 'asd@asd.com');
    
    commit;
    
    select *
      from my_users
     where rowid not in (select min(rowid) from my_users group by name, email);
    
    0 讨论(0)
  • 2020-11-21 13:46

    How we can count the duplicated values?? either it is repeated 2 times or greater than 2. just count them, not group wise.

    as simple as

    select COUNT(distinct col_01) from Table_01
    
    0 讨论(0)
提交回复
热议问题