Recipe · SQL recipes

Finding duplicates with GROUP BY and HAVING

Group by the columns that define a duplicate and use HAVING COUNT(*) > 1 to find the repeated keys.

Before continuing: This entry is part of the GROUP BY guide. If you are new to the topic, start with GROUP BY.

Query

This small set contains two repeated emails and one NULLvalue. Since the business rule only considers known emails, the example excludes NULL before grouping.

SQL verified in SQLite
WITH contacts(id, email) AS (
  VALUES
    (1, 'amy@example.test'),
    (2, 'liam@example.test'),
    (3, 'amy@example.test'),
    (4, 'megan@example.test'),
    (5, 'liam@example.test'),
    (6, NULL)
)
SELECT email, COUNT(*) AS occurrences
FROM contacts
WHERE email IS NOT NULL
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY email;

GROUP BY email forms one group per value. COUNT(*) counts its rows and HAVING keeps only the groups that appear more than once.

Cases that change the result

First define what “duplicate” means

Two rows can share a city, a date or a status without being duplicates. Repetition is only a problem when it affects a business key that should identify a single entity or event.

Repetition vs a business duplicate
CaseIs this necessarily a duplicate?Key you would check
Two customers live in LondonNoA city does not identify a customer
Two contacts share an email when emails must be uniqueYes, under that ruleemail
Two lines repeat the same product and order when that pair must be uniqueProbably(order_id, product_id)

The useful question is not “are there identical rows?” but “which columns should uniquely identify this data?”.

Duplicates defined by several columns

If identity depends on more than one column, group by all of them. For example, to find a repeated order and product combination:

Composite key
SELECT order_id,
       product_id,
       COUNT(*) AS occurrences
FROM order_items
GROUP BY order_id, product_id
HAVING COUNT(*) > 1;

Grouping only by product_id would answer a different question: how many lines use each product across all orders. The grouping level must match the uniqueness rule you want to check.

If you want to practise this idea in more depth, see GROUP BY with multiple columns.

How to get back the original duplicate rows

The aggregate query returns the repeated key and its count, not each original row. To inspect the full rows, find the repeated keys first and join them back to the source.

Two-step diagnosis
WITH repeated AS (
  SELECT email
  FROM contacts
  WHERE email IS NOT NULL
  GROUP BY email
  HAVING COUNT(*) > 1
)
SELECT c.id, c.email
FROM contacts AS c
JOIN repeated AS r
  ON r.email = c.email
ORDER BY c.email, c.id;

This second step is especially useful before fixing data: first identify the problem key, then review exactly which rows are involved.

Finding the repeated key does not decide which record to keep. If you need to fix the table, follow the full walkthrough on removing duplicates with ROW_NUMBER: which row survives, reviewing the ids, a dry run of the DELETE and a check with ROLLBACK.

What to do with NULL

When grouping, NULL values can end up together in one group. That does not mean several unknown values represent the same business value.

If an optional field can be NULL, decide explicitly whether to include it in the diagnosis. When looking for repeated emails, phone numbers or codes, it is often more useful to add WHERE column_name IS NOT NULL. If the business rule treats NULL as meaningful, do not filter it out.