STUDY GUIDE

How to check SQL joins and duplicate rows

Diagnose missing or duplicate rows in database coursework by checking join keys, result grain, and aggregations.

Decide what one result row represents

Write down whether your output should contain one row per customer, per order, or per line item. A customer with two orders can appear twice in a correct customer-to-orders join. That becomes a problem when your intended output is one row per customer.

Use a tiny dataset to test the join

Imagine three customers with respectively two, one, and zero orders. An inner join returns three order rows. A left join starting with customers returns four rows, including one with no matching order. Compare those predictions with your query before adding filters.

Check filters and counts separately

A WHERE clause requiring a matching value from the right table can remove the unmatched rows from a left join. After grouping by customer, COUNT(*) counts the unmatched row, while COUNT(order_id) counts only non-null order IDs. Choose the expression that matches the question.

Find where the row count changes

Run the base table query, add one join, then add filters and grouping in separate steps. Check keys and cardinality when a step changes the result unexpectedly. Investigate the source of duplicates before reaching for DISTINCT.

ASK ABOUT YOUR COURSE

Let’s work through it.

Tell us your course name, assignment or project, deadline, and what you have tried so far.