01 / 21

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.

SQL

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

Cardinality
The number of rows in a table or result set. Checking cardinality means checking the row count.
Grain
The level of detail each row represents: one row per customer, per order, or per line item.
Fan-out
When a join produces more rows than the original table because a key matches multiple rows on the other side.

Table Relationships and Row Counts

1 : 1 3 rows → 3 rows 1 : N 3 rows → 5 rows N : N 3 rows → 6 rows Table A Table B Table A Table B Table A Table B Row count after INNER JOIN depends on key relationships Duplicate keys cause fan-out: the result can exceed either input table

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

orders order_id | cust_id 101 | C1 102 | C1 103 | C2 addresses addr_id | cust_id A1 | C1 A2 | C1 A3 | C2 INNER JOIN ON cust_id order_id | cust_id | addr_id 101 | C1 | A1 101 | C1 | A2 102 | C1 | A1 102 | C1 | A2 103 | C2 | A3 fan-out! C1: 2 x 2 = 4 rows C2: 1 x 1 = 1 row Total: 5 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

One-to-Many (\(1:N\))
Key is unique in one table, not the other. Result rows = rows in the "many" side. Safe and predictable. Example: customers JOIN orders.
Many-to-Many (\(N:N\))
Key has duplicates in both tables. Row count can explode. Almost always a bug or a sign you need a bridge table. Example: tags JOIN categories on shared product_id.

The Validation Checklist

1
Count before: Run SELECT COUNT(*) on each source table to establish baselines.
2
Check key uniqueness: Use GROUP BY ... HAVING COUNT(*) > 1 on the join key in both tables.
3
Predict the result: Based on the relationship (1:1, 1:N, N:N) and join type, estimate the expected row count.
4
Count after: Run SELECT COUNT(*) on the joined result and compare to your prediction.
5
Investigate discrepancies: Check for NULLs in join keys, unexpected duplicates, or missing rows.

Example 1: Simple Validation

-- Step 1: Count before SELECT COUNT(*) FROM customers; -- 1,000 SELECT COUNT(*) FROM orders; -- 3,500 -- Step 2: Check uniqueness SELECT customer_id, COUNT(*) FROM customers GROUP BY customer_id HAVING COUNT(*) > 1; -- 0 rows (unique!)

Example 1: Verify the Result

-- Step 3: Run the join SELECT COUNT(*) FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id; -- Result: 3,500 ✓

Example 2: Catching a Fan-Out

-- Baseline counts SELECT COUNT(*) FROM orders; -- 3,500 SELECT COUNT(*) FROM promotions; -- 200 -- Join SELECT COUNT(*) FROM orders o INNER JOIN promotions p ON o.promo_code = p.promo_code; -- Result: 8,200 ← something is wrong!

Example 2: Fixing the Fan-Out

-- Diagnose: find duplicates SELECT promo_code, COUNT(*) AS cnt FROM promotions GROUP BY promo_code HAVING COUNT(*) > 1; -- SAVE20 appears 3 times, WELCOME10 twice... -- Fix: deduplicate before joining SELECT COUNT(*) FROM orders o INNER JOIN ( SELECT DISTINCT promo_code, discount_pct FROM promotions ) p ON o.promo_code = p.promo_code; -- Result: 3,500 ✓

Example 3: LEFT JOIN Validation

SELECT COUNT(*) FROM students; -- 500 SELECT COUNT(*) FROM scores; -- 480 SELECT COUNT(*) FROM students s LEFT JOIN scores sc ON s.student_id = sc.student_id; -- Expected: >= 500 (all students kept) -- Result: 500 ✓

Common Mistakes

✕ Skipping the Count
Assuming the join "just works" without verifying row counts leads to silently duplicated or dropped data.
✕ Ignoring NULLs in Keys
NULL never equals NULL. Rows with NULL join keys will not match in an INNER JOIN, silently dropping data.
✕ Wrong Join Key
Joining on a non-unique column by accident (e.g., name instead of id) creates a many-to-many explosion.

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?