---
name: predict-rows-before-joining-tables
description: "Predict how many rows a left join will return before running it, find the keys that multiply rows and the records left unmatched, and state the null-key rule used. Use when joining tables, merging spreadsheets or running a SQL or pandas join that might duplicate or drop rows."
license: CC-BY-4.0
metadata:
  author: Raq.com
  version: "2026-11-22"
  homepage: https://skills.raq.com/skills/predict-rows-before-joining-tables
---

# Check row counts before joining tables

Predict the size of a left join and pinpoint keys that would multiply records or leave a lookup empty.

## When to use this skill

- Someone is about to join two tables, merge two spreadsheets or add a lookup table and wants to know the output row count.
- A join returned more rows than the left table and someone wants to know why.
- Keys may be duplicated or blank on either side.

## When not to use it

- Checking a single spreadsheet lookup formula. Use the lookup table skill.
- Inner, right or full outer joins without restating the method. The counts here are for a left join, which keeps every left row.

## What you need

- Left and right key columns, including duplicates and blanks.
- Join type, match policy for null keys and expected relationship.

## Ask before you start

If any of these are missing or unclear, ask before doing the work.

- Which columns are the join keys on each side, and are they stored as the same type?
- Which join type and engine will run it, such as SQL, pandas or Power Query? They treat blank keys differently.
- Should a blank key ever match another blank key?
- What relationship do you expect, such as one customer to many orders? It shows which duplicates are legitimate.

## Method

1. Count how often each key occurs on each side. Keep key values in their declared type and treat blank as missing when the contract says so.
2. Look for differences the engine treats as distinct keys, such as text versus number, trailing spaces or case, and report them rather than silently fixing them.
3. Confirm the null policy. SQL equality never matches a null to a null, because NULL = NULL is unknown rather than true; pandas merge matches null keys to each other. State the policy before giving a row count.
4. For each key present on both sides, multiply the left count by the right count. Add one row for each unmatched left record, including blanks when nulls do not match. The sum is the expected output.
5. List each left key once where a right-hand duplicate creates extra rows, and count the left records without a match.
6. After the join, compare the actual row count with the prediction. In pandas, validate='many_to_one' stops a merge whose right-hand keys are not unique.

## Checks before you deliver

- Using the number of distinct keys hides multiplication, especially where both tables contain repeated keys.
- Removing duplicates to force a desired row count can discard legitimate records. Trace the relationship first.
- Assuming pandas and SQL agree. pandas matches null keys to each other; SQL equality does not, so unmatched counts can differ even when the row count is the same.
- Joining a key stored as text on one side to numbers on the other. Depending on the engine the keys fail to match or the join raises an error.

## How to present the result

Lead with the predicted output row count beside the left table's row count. Then list each key that multiplies rows with its count on each side and the extra rows it adds, followed by the unmatched left records and the null-key rule assumed. End with the check to run after the join.

## Worked example

**Request:** Predict a left join on id. Blanks never match.

**Supplied material:**

```text
left.id: A, A, B, C, <blank>
right.id: A, A, B, <blank>
```

**Answer:** Expect seven output rows from four left rows. The two A contacts each produce two matches. C and the blank key each remain as one unmatched row. Check whether both right-hand A records are legitimate before joining.

## Structured output for tests

For an ordinary job, answer in the format above and point back to the supplied evidence. Use this structure only for the test cases published with this skill, or when someone asks for structured JSON.

Return left_rows, joined_rows and unmatched_left_rows as integers, plus multiplied_keys as sorted strings. Represent a matched null key as null, after all strings. Calculate a left join with the null policy specified by the user.

## Sources

- [pandas: merge](https://pandas.pydata.org/docs/reference/api/pandas.merge.html): The reference describes a left join as keeping keys from the left frame, offers validate checks such as many_to_one, and warns that rows with null keys on both sides are matched to each other, unlike usual SQL join behaviour.
- [PostgreSQL: comparison functions and operators](https://www.postgresql.org/docs/current/functions-comparison.html): PostgreSQL documents that ordinary comparison with a null yields null rather than true, so NULL is not equal to NULL, while IS NOT DISTINCT FROM treats two nulls as equal. A SQLite check of the first case gave the same seven rows with two unmatched.

Test cases and an answer checker for this skill: https://skills.raq.com/skills/predict-rows-before-joining-tables
