5

I have a database that stores first and last names with a user id. The table looks like this:

uid value
1   Fred
1   Keller
2   Tim
2   LaChef
3   Adam
3   Adam

Having a duplicate uid is fine, but I want to find all entries that have the same first and last name though? Like uid 3. Any SQL ideas?

Dmytro Maslenko
  • 2,247
  • 9
  • 16
Adam Esterle
  • 343
  • 2
  • 4
  • 13

2 Answers2

13

Try this:

SELECT uid FROM tablename 
GROUP BY uid, name HAVING COUNT(*) = 2;
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
4

To return just a single copy of each "duplicate", then:

SELECT t.uid
     , t.value
  FROM mytable t
 GROUP
    BY t.uid
     , t.value
HAVING COUNT(1) > 1
 ORDER
    BY t.uid
     , t.value

To return "all" entries that are duplicates, rather than just one copy, and if you don't need to return any NULL values, then:

SELECT a.uid
     , a.value
  FROM mytable a
  JOIN ( SELECT t.uid
              , t.value
           FROM mytable t
          GROUP
             BY t.uid
              , t.value
         HAVING COUNT(1) > 1
       ) d
     ON d.uid = a.uid
    AND d.value = a.value
  ORDER
     BY a.uid
      , a.value

If you do want to return NULL (where the NULL is a duplicate), then change the comparison operators in the ON clause to the null-safe equality comparison: <=>

     ON d.uid <=> a.uid
    AND d.value <=> a.value
spencer7593
  • 106,611
  • 15
  • 112
  • 140
  • <=> produces an error on SQL SERVER 2012, nonetheless if ANSI_NULLS is ON, it won't considere NULL duplicates – Mauricio Quintana Apr 30 '15 at 22:18
  • @MauricioQuintana: Yes. The **`<=>`** null-safe comparison operator is a MySQL extension to the SQL standard. That operator isn't supported on SQL Server. (The question was tagged with "mysql", not "sql-server".) OP said he wanted to identify **"all entries that have the same first and last name"**. OP may want to consider a NULL "the same" as a NULL. Multiple rows with `NULL` values will be "grouped" together by a `GROUP BY`, and the individual rows do contribute to the count. The issue addressed by the **`<=>`** isn't about duplicates; it's about returning rows that have `NULL` values. – spencer7593 Apr 30 '15 at 23:59
  • @MauricioQuintana: The behavior we get in MySQL with **`a <=> b`** can be emulated in SQL Server by writing **`(a=b OR (a IS NULL AND b IS NULL))`**. – spencer7593 May 01 '15 at 00:02