Lesson 10 of 12
Structured learning draftFind duplicate identifiers before reporting
A repeated sale identifier can make a report appear to contain more business events than actually occurred. Test identifier uniqueness before aggregating, and distinguish a legitimate repeated customer from an invalid repeated sale key.
Learning objectives
- Check a candidate identifier for duplicates.
- Distinguish event keys from customer names.
- Avoid hiding duplicates with an unexplained DISTINCT.
Find duplicate identifiers before reporting: the working sequence
Trace the three stages of find duplicate identifiers before reporting and compare them with the example.
The input contains three rows but repeats sale identifier 1.
Grouping by the sale identifier produces a count per event key.
The duplicate check flags key 1 for investigation before totals are trusted.
The check groups by the identifier and counts rows in each group. HAVING COUNT(*) > 1 identifies repeated keys. It finds a data-quality symptom; deciding which record is authoritative requires a documented correction policy.
WITH incoming(id, customer) AS (
VALUES (1, 'Amina'), (1, 'Amina'), (2, 'Brian')
)
SELECT id, COUNT(*) AS occurrences
FROM incoming
GROUP BY id
HAVING COUNT(*) > 1
ORDER BY id;id | occurrences
1 | 2Try it, then change it
- Run the check and identify both input rows for key 1.
- Add two different customers with the same display name and explain why names are not event identifiers.
- Compare SELECT DISTINCT with the original input and explain which information it hides.
Common mistakes
- Treating repeated customer names as duplicated sales.
- Using DISTINCT to hide a duplicate without finding its cause.
- Deleting a conflicting row without an authoritative source.
Practice activity
Apply the lesson
Create a fixture with one exact duplicate and one conflicting record sharing the same key. Write a query to locate the keys and describe how each case needs investigation. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · databases
A join returns more rows than expected.
- Find the cause
- Count matching records for each join key in both tables. A key repeated on both sides creates multiple combinations.
- Try this
- Decide the intended row grain first: one row per customer, order or item. Aggregate at that grain or correct the join condition; do not hide the problem with DISTINCT.
- Check the fix
- Use two customers and three orders as a fixture. Predict every result row and check unmatched records separately.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: databases with SQL
Watch Querying or Relating to see selection and joins explained using actual tables. Examples begin with SQLite; check syntax when using PostgreSQL.
Try it yourself: Draw two small tables, predict the joined rows, then compare your prediction with the query result.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
What does the repeated identifier check establish?
Lesson summary
- Uniqueness checks depend on the correct key.
- Detection does not establish which record should be retained.
- Investigate duplicate inputs before calculating event totals.
Sources and further reading
- Grouped table expressionsPostgreSQL Global Development Group - accessed 2026-10-07
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.