0

I would like to select all duplicate from a table:

SELECT * FROM people HAVING (count(*) OVER (PARTITION BY name)) > 1;

Unfortunately I get the error:

Error Code: 3593. You cannot use the window function 'count' in this context. 

One less elegant solution would be:

SELECT 
    *
FROM
    people
WHERE
    code IN (SELECT 
            name
        FROM
            people
        GROUP BY name
        HAVING COUNT(*) > 1);

How can I rewrite my first query to make it work?

nowox
  • 25,978
  • 39
  • 143
  • 293

1 Answers1

1

If the code is identical then you can use exists :

select p.*
from people p 
where exists (select 1 from people p1 where p1.name = p.name and p.code <> p1.code);

Use identity column instead if code column doesn't have a identity feature if entire table has no any identity column then your method works fine with following updated query :

SELECT p.*
FROM people p
WHERE name IN (SELECT name FROM people GROUP BY name HAVING COUNT(*) > 1);
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52