I would like to delete rows from my database that have the same name. I’ve checked Stackoverflow and found something like this:
DELETE
FROM my_table mt1 USING my_table mt2
WHERE mt1.my_name = mt2.my_name AND mt1.unique_id<mt2.unique_id;
This of course works but leaves one row. I have a request:
If there are rows with duplicate rows I have to remove ALL of them (not leave one).
>Solution :
We can use GROUP BY with a HAVING clause as subquery:
DELETE
FROM my_table
WHERE my_name IN
(SELECT
my_name
FROM
my_table
GROUP BY
my_name
HAVING
COUNT(*) > 1);
Try out: db<>fiddle