3

I have table with one column, and I need to get all duplicated lines. This is table:

|  name  |
|  ----- |
|    a   |
|    a   |
|    b   |
|    c   |
|    c   |
|    d   |

And this is expected result set:

|  name  |
|  ----- |
|    a   |
|    a   |
|    c   |
|    c   |

I already do this that way:

 SELECT t.name FROM my_table t inner join (
        SELECT name, count(1) AS count FROM my_table t2 GROUP BY t2.name HAVING count > 1
      ) AS t1 on t1.name = t.name

Is it possible to do it without subquery?

degr
  • 1,469
  • 1
  • 16
  • 35

2 Answers2

3

If you have only one column, wouldn't this output do?

select name, count(*) as num_duplicates
from t
group by name
having count(*) > 1;

This produces one row per name with the number of duplicates. If you have only one column, that seems like a better output (to me) than the list of names repeated.

Nimantha
  • 5,793
  • 5
  • 23
  • 56
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
1

you need to first bring name of having count >1

select * from my_table where name in 
(select name from my_table having count(name) > 1 group by name )
krishn Patel
  • 2,549
  • 1
  • 18
  • 29