# JOIN reconciliation workbook

**Your name or initials (optional):** __________ **Date:** __________

Keep answers locally. Use fictional records only. You may use text lists instead of drawing. Save your initial prediction before execution and explain every revision. The answer pack is separate: `SOLUTIONS.md`.

## A. Readiness check (before starting)

Explain in your own words: a table row; a primary key; a GROUP BY result; SUM versus COUNT; why 0 and NULL differ. Write a SELECT that reads order IDs and amounts without joining. If you cannot yet do these, return to earlier SQL teaching rather than guessing through this unit. No earlier SQL unit is included here.

## B. Guided base case

Use only `base`. The complete records are in UNIT.md and data/base.sql.

**G1. Grain and keys.** Write one sentence defining a row in each table. Identify primary and foreign keys. Explain whether customer_id uniquely identifies an order.

Initial answer: __________

Correction and reason: __________

**G2. Predict expansion.** Without running SQL, list every INNER JOIN result as `(order_id,item_id)`. Then list every LEFT JOIN result. Predict each row count and identify any parent repeated or excluded. Run `inner_rows`/`left_rows` only afterwards.

Prediction: __________ Observed: __________ Why: __________

**G3. Diagnose totals.** Write an all-order sum query and compare it with raw inner/left joined sums. Reconcile differences by naming exactly which order values were repeated and omitted. Why would matching one expected total not, by itself, prove the method?

Queries and reconciliation: __________

**G4. Three counts.** Predict `COUNT(*)`, `COUNT(i.item_id)` and `COUNT(DISTINCT o.order_id)` for each join. Explain why the results answer different questions. Then produce one row per order with its number of recorded items, including orders with none.

Predictions, SQL and meanings: __________

**G5. Match the question.** Write one query for all recorded order value and two equivalent queries for the value of orders with at least one recorded item. At least one should use EXISTS. Explain why two matches do not give two outer contributions.

SQL and explanation: __________

**G6. Denominators.** Compute the all-order mean and the item-bearing mean. State numerator, denominator, unit and rounding. Diagnose the raw INNER JOIN mean.

Answer: __________

## C. Counterexample and data-quality extensions

**E1. Equal values.** Reset by selecting `equal` (not `base`). This is base plus order `(105,D,100)` and item `(5,105)`. Compare the correct all-order value, correct item-bearing value, raw inner/left sums and SUM(DISTINCT amount_cad). Explain why equal numbers do not mean the same entity. Does adding a correct COUNT(DISTINCT order_id) alongside a wrong SUM repair the SUM?

Initial prediction: __________ Observed: __________ Explanation: __________

**E2. Orphan input.** Reset to `staging`. Inspect the raw staging row `(9,999)`. Write a query identifying staging items with no parent order. Contrast this with an order having no items. Recommend a cautious handling step without inventing a reason or silently discarding evidence.

SQL, interpretation and action: __________

## D. Independent case: a different community stall

Use only `independent`. Do not modify or combine it with the base fixture.

| order_id | customer_id | amount_cad |
|---:|:---|---:|
| 201 | D | 50 |
| 202 | E | 50 |
| 203 | D | 25 |
| 204 | F | 0 |
| 205 | G | 75 |

| item_id | order_id |
|---:|---:|
| 11 | 201 |
| 12 | 201 |
| 13 | 202 |
| 14 | 204 |
| 15 | 204 |
| 16 | 204 |

**I1. Counts before execution.** Predict the inner/left output rows, distinct matched orders, matched item rows and all-order count. Identify every order without a recorded item.

Prediction: __________ Observed: __________ Correction: __________

**I2. Values and means.** Write correct queries for all-order value, item-bearing value and both means. Compare both raw joined sums and SUM(DISTINCT amount_cad). Explain the role of the zero-valued order and the two equal-valued orders. Retain unrounded means in your working.

SQL/result/reason: __________

**I3. Empty populations.** Predict what each of SUM, COUNT and AVG should show if there are no matching orders. Explain why a reporting choice to display total zero is not a reason to report mean zero. This is a reasoning task; the supplied automated suite checks empty/no-item cases without requiring you to modify data.

Answer: __________

## E. Mini-project: a reconciliation note

A fictional coordinator wants: (a) all recorded order value; (b) value and mean for orders with recorded items; (c) IDs needing item-record follow-up. They have proposed summing order amounts after a LEFT JOIN. Use the **independent** fixture.

Create a concise one-page note plus your `.sql` files and results. Include:
- The exact population for each requested measure, table grain and relationship.
- Their faulty query/result and corrected queries/results.
- An entity-level reconciliation, including zero/equal amounts and missing child rows.
- Numerator/denominator/rounding for each mean.
- A changed-data test that would expose a plausible but wrong repair.
- At least three limits of the records and a cautious follow-up request.
- Fixture name, runtime version if executed, reproduction command and correction log.

Do not claim recognized revenue, profits, payment, fulfillment or reasons for missing items. Equivalent SQL is acceptable if it preserves the required population. A short accurate note is better than filling a word quota.

| Criterion | Weight | Evidence needed |
|---|---:|---|
| Grain and relationship | 25 | Correct entities/keys; expansion and missing-match explanation |
| Queries and outputs | 30 | Correct populations, totals, means; executable/reproducible answer |
| Reconciliation and limits | 25 | Specific wrong-result explanation, counterexample, justified limits |
| Reproducibility and communication | 20 | Fixture/version/queries/results, clear note, visible revisions |

Self-check bands: complete = correct result plus valid reasoning and evidence; partial = some correct outputs but missing population or reproducibility; not yet = fan-out/denominator error remains. Syntax alone cannot earn full correctness. No external marking service is implied.

## F. Delayed transfer (later day, first attempt without solutions)

Use `delayed`. Orders: `(301,H,30)`, `(302,I,90)`, `(303,J,30)`. Items: `(21,301)`, `(22,302)`, `(23,302)`.

**D1.** Predict counts and faulty inner/left sums; produce correct totals and means. Explain why a DISTINCT-amount shortcut may look successful for one requested measure but fail for another. Identify the missing-child order without inventing a cause.

**D2.** Write a five-sentence verification checklist for a future dataset with a different number of items per order. Mark which checks concern SQL execution and which concern interpretation. Compare your first attempt against the key, then explain one durable rule you will use again.

Date / initial answer / later correction: __________
