Identifying
Duplicates in SQL
Learn to detect duplicate rows and unexpected multiplicity in your data using GROUP BY, HAVING, and COUNT.
What Are Duplicates?
A duplicate row is a row that shares the same values for one or more key columns as another row in the same table. Duplicates often appear after messy data imports, faulty JOINs, or missing primary keys.
Why Duplicates Matter
Undetected duplicates silently corrupt every calculation downstream. A single duplicated order row can inflate revenue, distort averages, and break dashboard trust.
Seeing Duplicates in Action
The Core Duplicate Detection Pattern
Every duplicate check follows the same three-part template: SELECT the key columns, GROUP BY them, then \(\texttt{HAVING COUNT}(*) > 1\) to keep only groups with more than one row.
GROUP BY + HAVING Explained
WHERE vs HAVING
Students often confuse WHERE and HAVING. Both filter data, but they operate at different stages of query execution.
| WHERE | HAVING | |
|---|---|---|
| Filters | Individual rows | Groups (after GROUP BY) |
| Runs | Before grouping | After grouping |
| Can use aggregates? | No | Yes |
| Example | WHERE status = 'active' |
\(\texttt{HAVING COUNT}(*) > 1\) |
Unexpected Multiplicity
Multiplicity means a key that you expected to be unique actually maps to multiple rows. This commonly happens when you JOIN tables and a "unique" key turns out to have duplicates in one table.
Step-by-Step Approach
Example 1: Basic Duplicate Check
A students table should have one row per student. Find any email addresses that appear more than once.
Example 2: Multi-Column Key
An enrollments table records which student is enrolled in which course. The combination of (student_id, course_id) should be unique: no student enrolled twice in the same course.
Example 3: Detecting Join Fanout
Before joining two tables, verify the join key is unique in the table you expect to be the "one" side. If it's not unique, the JOIN will multiply rows.
Quick Check: Row Count Validation
A fast sanity check compares the total row count against the count of distinct key values. If they differ, duplicates exist.
DISTINCT vs GROUP BY
Both can remove duplicates from output, but they serve different purposes. Understanding when to use each is essential.
| SELECT DISTINCT | GROUP BY + HAVING | |
|---|---|---|
| Purpose | Remove duplicate rows from output | Find and count duplicate groups |
| Shows count? | No | Yes |
| Filters duplicates? | Hides them silently | Exposes them explicitly |
| Best for | Quick dedup of output | Data quality investigation |
Common Mistakes
Summary: Your Toolkit
| Task | SQL Pattern | When to Use |
|---|---|---|
| Find duplicates | \(\texttt{GROUP BY} \dots \texttt{HAVING} > 1\) | Data quality audit |
| Quick count check | \(\texttt{COUNT}(*) \text{ vs } \texttt{COUNT(DISTINCT)}\) | Before any join |
| Remove from output | SELECT DISTINCT |
Clean report output |
| Find join fanout | \(\texttt{GROUP BY join\_key + HAVING}\) | Debugging row explosion |
| Delete duplicates | \(\texttt{ROW\_NUMBER}() + \texttt{DELETE}\) | Permanent data cleanup |
Question 1 of 5
Which SQL clause is used to filter groups based on an aggregate function like \(\texttt{COUNT}(*)\)?
Question 2 of 5
A table has 1,000 rows but only 950 distinct values for order_id. What does this tell you?
Question 3 of 5
What happens if you write \(\texttt{WHERE COUNT}(*) > 1\) instead of \(\texttt{HAVING COUNT}(*) > 1\)?
Question 4 of 5
You are joining two tables:
orders (1,000 rows, one row per order)
customers (500 rows, but customer_id has duplicates)
The join is: orders JOIN customers ON orders.customer_id = customers.customer_id
What is the most likely result of this JOIN?
Question 5 of 5
Consider this query:
The query returns zero rows, but you know there are students enrolled in the same course twice. What is the most likely problem?