Is it possible for SQL to find duplicate records?

Can I use an SQL query to search for records where one field is the same in both? That is, can I use the following table and return 1,3 (identifiers) by comparing the columns of names (and ignoring the phone)?

    ID | Name | Phone

    1 | Bob | 5555555555
    2 | John | 1234567890
    3 | Bob | 1515151515
    4 | Tim | 5555555555
+5
source share
3 answers

To get all the names that exist more than once, you can run this statement:

SELECT Name FROM People GROUP BY Name HAVING COUNT(*)>1;
+10
source

To get the duplicate identifiers "1,3", which are combined in this way, use GROUP_CONCAT :

SELECT GROUP_CONCAT( ID SEPARATOR ',' )
FROM Table
GROUP BY Name
HAVING COUNT(*) > 1
+2

- :

SELECT P1.Id, P2.Id
  FROM People P1, People P2
 WHERE P1.Id < P2.Id
   AND P1.Name = P2.Name;

The first condition guarantees that you see only a pair (1,3), and not extraneous pairs (3,1) or identical lines (1,1), (3,3).

0
source

All Articles