Lawfare Academy

Data Management and SQL for Business · Chapter 3 of 6

Filtering, aggregation and meaningful zeroes

Return to the linked course unit →

DEVELOPED COURSE TEXT

Filtering, aggregation and meaningful zeroes

Filter rows and groups at the intended stage

WHERE selects rows before aggregation; HAVING selects groups after aggregate values are formed. Moving a condition between them can change the question. A status filter is not equivalent to filtering by an aggregate payment total. Specify whether the query should include customers with no eligible orders and whether empty results are meaningful. The SQL engine evaluates the written query, not the business title above it.

COUNT, NULL and empty sums have distinct meanings

COUNT(*) counts rows; COUNT(expression) counts non-NULL values of that expression. After an outer join, the placeholder row for an unmatched customer can make COUNT(*) equal one even though no order exists. In SQLite, SUM over no matching non-NULL input values returns NULL; a displayed zero requires an explicit interpretation such as COALESCE. Unknown is not always zero. Choose the replacement because the metric contract justifies it, not merely to make the dashboard look tidy.

Paid order count and line amount

SELECT COUNT(DISTINCT o.order_id) AS paid_orders,
       COUNT(*) AS item_rows,
       SUM(i.quantity * i.unit_price_cents) AS line_amount_cents
FROM orders AS o
JOIN order_items AS i ON i.order_id = o.order_id
WHERE o.status = 'paid';

Empty aggregation example

SELECT COUNT(*), SUM(amount_cents), COALESCE(SUM(amount_cents),0)
FROM payments WHERE order_id=999;

Worked records

Record 1

The paid-order query joins eligible orders to their items. Order 1 produces two rows; orders 3 and 5 produce one each. A separate payment query looks for nonexistent order 999.

Develop the reasoning

There are four joined item rows but three distinct paid orders. The line-amount sum is 5,600 cents. COUNT(*) correctly reports item rows in this query but would be wrong if labelled order count. In the empty payment example, COUNT(*) is zero, SUM is NULL and the explicitly coalesced sum is zero. That final zero is an analytical choice matching a no-recorded-payments interpretation in this fixture, not proof that the underlying real-world amount is known to be zero. Missing or incomplete ingestion would require a different interpretation.

Progressive practice — optional

1. Foundation

Why does COUNT(*) return four for the paid item join?

Compare the worked reasoning

The query’s row grain is eligible order-item combinations, not orders.

Change the record

Every paid order has exactly one item in another dataset.

Compare the changed-record reasoning

The two counts happen to agree there; that coincidence does not make the query robust to multiple items.

2. Application

Can COALESCE always turn missing financial evidence into a valid zero?

Compare the worked reasoning

No. It changes the representation; the business interpretation must be justified.

Change the record

The source ingestion process is incomplete.

Compare the changed-record reasoning

A coalesced zero could conceal unknown data and should be accompanied by completeness controls.

3. Transfer

A manager wants groups whose total exceeds a threshold. Is a row-level price filter equivalent?

Compare the worked reasoning

No. Filtering individual prices before summing changes the eligible rows rather than selecting groups by their total.

Change the record

Both queries happen to match on one tiny fixture.

Compare the changed-record reasoning

Use counterexamples and a precise contract; one agreement does not establish equivalence.

Sources and scope