Skip to content

Instantly share code, notes, and snippets.

@6ui11em
Last active February 15, 2022 11:05
Show Gist options
  • Save 6ui11em/cbcfa585c231aad64e8a8092c6ddc3dd to your computer and use it in GitHub Desktop.
Save 6ui11em/cbcfa585c231aad64e8a8092c6ddc3dd to your computer and use it in GitHub Desktop.
SQL: Find duplicate values #mysql #sql #select #duplicate
SELECT
col,
COUNT(col)
FROM
table_name
GROUP BY col
HAVING COUNT(col) > 1;
---
SELECT
col1, COUNT(col1),
col2, COUNT(col2),
...
FROM
table_name
GROUP BY
col1,
col2, ...
HAVING
(COUNT(col1) > 1) AND
(COUNT(col2) > 1) AND
...
---
-- Get duplicates with column and row values
select s.id, t.*
from [stuff] s
join (
select name, city, count(*) as qty
from [stuff]
group by name, city
having count(*) > 1
) t on s.name = t.name and s.city = t.city
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment