How to Find Duplicate Rows in SQL (GROUP BY + HAVING)

Find duplicate values with GROUP BY and HAVING COUNT(*) > 1, then return every duplicate row with a window function.

rows Austin Milan Austin Milan GROUP BY Austin → COUNT = 2 Milan → COUNT = 2
GROUP BY buckets rows that share a value, then aggregates each bucket.

Which values repeat?

SELECT city, COUNT(*) AS n
FROM customers
GROUP BY city
HAVING COUNT(*) > 1;

One row per duplicated key. Perfect when you only need “which cities appear more than once?”

Return every duplicate row

SELECT *
FROM (
  SELECT c.*,
         COUNT(*) OVER (PARTITION BY city) AS n
  FROM customers c
) x
WHERE n > 1;

Window COUNT keeps every matching customer row — useful when you need the full list of duplicates, not just the city names.

-- Example result idea
-- Austin appears twice → both Austin customers returned
-- Milan appears once  → filtered out

Duplicates across multiple columns

List every column that should be unique together in GROUP BY / PARTITION BY:

GROUP BY first_name, last_name, city
HAVING COUNT(*) > 1

Self-joins for duplicates work but explode row counts — prefer GROUP BY or windows.

FAQ

How do I find duplicates in SQL?

GROUP BY the columns that define a duplicate, then HAVING COUNT(*) > 1. Use a window COUNT(*) OVER (PARTITION BY …) when you need every duplicate row back.

Try it yourself

The fastest way to lock this in is a short drill on the same Northstar Market tables.

Practice GROUP BY

Related guides