Validating
Row Counts
Learn to verify that your SQL joins produce the expected number of rows and catch data issues before they cascade through your analysis.
Why Row Counts Matter
Every time you join two tables, the number of rows in your result can increase, decrease, or stay the same depending on the relationship between the tables and the type of join you use. A single duplicated key can silently multiply your data and corrupt every downstream calculation.
Key Terminology
Table Relationships and Row Counts
The Row Count Rule
If the join key is unique in at least one of the tables (e.g., Table A), no fan-out will occur from that table's side. The result row count will then be at most the number of rows in the other table (Table B).
How Duplicates Multiply Rows
Join Types and Row Counts
| Join Type | Unmatched Left | Unmatched Right | Row Count |
|---|---|---|---|
| INNER JOIN | Dropped | Dropped | Depends on uniqueness* |
| LEFT JOIN | Kept (NULLs) | Dropped | \( \geq |A| \) |
| RIGHT JOIN | Dropped | Kept (NULLs) | \( \geq |B| \) |
| FULL OUTER JOIN | Kept (NULLs) | Kept (NULLs) | \( \geq \max(|A|, |B|) \) |
| CROSS JOIN | N/A | N/A | \( |A| \times |B| \) |
\(1:N\) vs \(N:N\) Joins
The Validation Checklist
Example 1: Simple Validation
Example 1: Verify the Result
Example 2: Catching a Fan-Out
Example 2: Fixing the Fan-Out
Example 3: LEFT JOIN Validation
Common Mistakes
Validation Quick Reference
| Check | SQL Pattern | What It Tells You |
|---|---|---|
| Baseline count | SELECT COUNT(*) FROM table | Starting row count |
| Key uniqueness | GROUP BY key HAVING COUNT(*) > 1 | Duplicate keys exist |
| Post-join count | SELECT COUNT(*) FROM joined | Fan-out or data loss |
| NULL keys | WHERE key IS NULL | Unmatchable rows |
| Orphaned rows | LEFT JOIN ... WHERE b.key IS NULL | Rows with no match |
Question 1 of 5
Table A has 100 rows and Table B has 50 rows. The join key is unique in both tables and every key in B exists in A. How many rows does an INNER JOIN produce?
Question 2 of 5
A key value appears 3 times in Table A and 4 times in Table B. How many rows does this single key contribute to the INNER JOIN result?
Question 3 of 5
You run a LEFT JOIN from a 1,000-row table onto a 500-row table (1:1 keys, all unique). The result has 1,000 rows. What can you conclude?
Question 4 of 5
Which SQL query best detects duplicate join keys in a table before performing a join?
Question 5 of 5
An orders table (5,000 rows) is INNER JOINed to a payments table (4,800 rows) on order_id. The result has 5,300 rows. What is the most likely cause?