Preserved original teaching integrated with complete current on-site preparation. Independent human subject review and observed learner testing remain pending.
Start with a reporting question and a row grain
A collections dashboard must show every customer, even customers with no paid invoice, and report what is known about paid amounts. The desired row grain is one row per customer. That is not automatically the grain of a joined table: a customer can have several invoices, and an invoice can have several line items. Write the grain and qualifying condition before choosing joins or aggregates.
Use four customers A, B, C and D. A has a paid invoice for 100 and an open invoice for 50. B has an open invoice for 80. C has no invoice. D has a paid invoice whose amount is NULL because the amount has not been recovered from the import. The known paid amount is 100; the complete paid total is not known. The distinction between no paid invoice and a paid invoice with unknown amount matters to both operations and accounting. Treat the numbers as synthetic units rather than a currency ledger.
The predicate position changes who survives
Start with customers LEFT JOIN invoices ON invoices.customer_id=customers.id AND invoices.status='paid'. The qualification belongs to the join because customers without a qualifying invoice must remain. A has its paid row; B and C receive NULL-extended invoice columns; D keeps its real paid invoice with a missing amount. The join result contains one row for each customer in this fixture.
Now move status='paid' to WHERE after a LEFT JOIN based only on customer_id. B's open invoice fails the predicate. C's NULL-extended status does not make the predicate true, so C disappears too. A and D remain. A left join does not guarantee all left rows survive predicates that subsequently exclude their joined results. Adding OR invoices.id IS NULL would retain C but still omit B, whose nonqualifying open row prevented a NULL-extended join result. Placement must implement the reporting question, not merely contain the words LEFT JOIN.
Count an identity, count an amount, and name the difference
For the ON-filtered join, COUNT(*) is one for A, B, C and D because every group has one result row. COUNT(invoices.id) is [1,0,0,1], distinguishing matched paid invoices from NULL-extended rows. COUNT(invoices.amount) is [1,0,0,0], because D's real invoice has a NULL amount. SUM(amount) is [100,NULL,NULL,NULL]. SQLite aggregates skip NULL inputs; a sum with no non-NULL input remains NULL.
Blindly applying COALESCE(SUM(amount),0) reports zero for B, C and D. That may be an acceptable display rule for no qualifying invoices, but it makes D's missing amount look like a known zero. Carry the matched-invoice count and unknown-amount count alongside the known subtotal. If matched count is zero, a no-paid-invoice status can be displayed; if matched count exceeds non-NULL amount count, report an incomplete amount. Even a mixture of one known amount and one unknown amount can produce a plausible nonzero sum that is only a subtotal.
NULL also changes exclusion logic
Suppose an exclusion list contains customer 1 and an accidental NULL. A predicate customer_id NOT IN (SELECT customer_id FROM exclusions) does not select every customer other than 1. For an unlisted non-NULL candidate, the comparison with the NULL list member introduces an unknown truth value, and WHERE retains only true predicates. In this fixture the query returns no customers.
A correlated NOT EXISTS query asks a different, explicit question: retain the customer when no exclusion row has an equal identifier. It returns B, C and D. This does not make NOT EXISTS universally preferable regardless of business semantics. If a NULL exclusion row means the entire exclusion register is untrusted, the correct response may be to stop the report and repair the register. First determine the intended meaning, then choose SQL that implements it and validate the input.
A strong query review therefore includes a tiny counterexample fixture, not only a large production total. Include an unmatched customer, a customer with nonqualifying invoices, a real qualifying invoice with a missing measure, and more than one qualifying invoice. Check who survives, matched cardinalities, unknown measures and the final row grain. A grand total can be correct while the membership or attribution is wrong. Keep correctness checks separate from execution-plan tuning: an index cannot repair a predicate that asks the wrong question.
A reporting contract that survives missing measures
A defensible result has columns for customer identity, paid_invoice_count, unknown_paid_amount_count and known_paid_subtotal. For the fixture: A=(1,0,100), B=(0,0,NULL), C=(0,0,NULL), D=(1,1,NULL). The presentation layer may display a no-qualifying-invoice zero for B and C while marking D incomplete. If D later receives a paid invoice for 30 in addition to its unknown one, D becomes (2,1,30): the known subtotal is 30, not an established total of 30.
Progressive independent practice
Write your analysis before opening each model. Then change the stated fact and revise the reasoning.
Guided
Compare the customers returned when the paid-status predicate is in ON versus WHERE.
Compare the model reasoning
ON retains A, B, C and D. WHERE retains A and D. B has only an open invoice; C has no invoice. Both fail the post-join paid predicate.
Changed conditions
Add OR invoices.id IS NULL to WHERE.
Compare the revised analysis
C returns, but B still does not because its open invoice is a real joined row. The expression still fails the intended all-customer contract.
Independent
Explain why COUNT(*)=1, COUNT(invoice.id)=1 and COUNT(amount)=0 can occur for D. Specify a display policy.
Compare the model reasoning
D has one joined real invoice, whose identifier is non-NULL but whose amount is NULL. Mark the paid amount incomplete, rather than translating missing measurement into a known zero.
Changed conditions
D also has a paid invoice for 30.
Compare the revised analysis
Now count(*)=2, matched count=2 and non-NULL amount count=1. SUM=30 is a known subtotal; the complete total is still unknown.
Transfer
An exclusion subquery contains one valid key and NULL. Why does NOT IN return no eligible customers, and what must be decided before replacing it?
Compare the model reasoning
NULL makes the comparison unknown for otherwise unlisted candidates. Determine whether NULL means an absent key to reject/quarantine or a reason to invalidate the report. A correlated NOT EXISTS equality expresses no equal exclusion row when that is the intended rule.
Changed conditions
The customer identifier itself can be NULL.
Compare the revised analysis
Specify whether such customers are eligible and whether identifiers are valid at that grain. Do not rely on incidental NULL comparison behavior to define identity or eligibility.