Lawfare Academy

Data Management and SQL for Business · Chapter 1 of 6

Model business meaning before writing a query

Return to the linked course unit →

DEVELOPED COURSE TEXT

Model business meaning before writing a query

Choose a grain and a metric contract

A table’s grain states what one row represents. A customer, order, line item, payment and refund are different business events or entities. Combining them without specifying grain can make a syntactically valid query answer the wrong question. Define the eligible population, status, date basis, currency and treatment of cancellations and refunds before calculating a metric. An order’s line amount, a cash receipt and accounting revenue are not interchangeable. The fictional data below use integer cents in one currency and omit tax, fees and revenue-recognition rules.

A relational model separates distinct facts

A customer can place many orders, an order can contain many items and an order can receive several payments. Keeping these relationships separate avoids repeating a whole customer record on each item and supports independent constraints. Normalization does not mean splitting every field into another table; it means representing dependencies deliberately. The model still needs a business definition of which record is authoritative and how changes over time are represented. A current status field alone does not reconstruct its entire history.

Complete reproducible fixture

Run this SQL in a fresh SQLite database. It defines four customers, five orders, six items, four payments and one refund. Dates classify the order cohort; payments do not have payment dates in this intentionally limited fixture. Therefore it cannot answer an actual cash-received-during-January question.

PRAGMA foreign_keys = ON;
CREATE TABLE customers (
 customer_id INTEGER PRIMARY KEY,
 name TEXT NOT NULL,
 region TEXT NOT NULL
);
CREATE TABLE orders (
 order_id INTEGER PRIMARY KEY,
 customer_id INTEGER NOT NULL REFERENCES customers(customer_id),
 status TEXT NOT NULL CHECK(status IN ('paid','unpaid','cancelled')),
 created_date TEXT NOT NULL
);
CREATE TABLE order_items (
 item_id INTEGER PRIMARY KEY,
 order_id INTEGER NOT NULL REFERENCES orders(order_id),
 sku TEXT NOT NULL,
 quantity INTEGER NOT NULL CHECK(quantity > 0),
 unit_price_cents INTEGER NOT NULL CHECK(unit_price_cents >= 0)
);
CREATE TABLE payments (
 payment_id INTEGER PRIMARY KEY,
 order_id INTEGER NOT NULL REFERENCES orders(order_id),
 amount_cents INTEGER NOT NULL CHECK(amount_cents > 0)
);
CREATE TABLE refunds (
 refund_id INTEGER PRIMARY KEY,
 order_id INTEGER NOT NULL REFERENCES orders(order_id),
 amount_cents INTEGER NOT NULL CHECK(amount_cents > 0)
);
CREATE TABLE inventory (
 sku TEXT PRIMARY KEY,
 on_hand INTEGER NOT NULL CHECK(on_hand >= 0)
);
INSERT INTO customers VALUES
 (1,'Alpha','North'),(2,'Beta','South'),
 (3,'Gamma','North'),(4,'Delta','West');
INSERT INTO orders VALUES
 (1,1,'paid','2026-01-01'),(2,1,'unpaid','2026-01-02'),
 (3,2,'paid','2026-01-03'),(4,3,'cancelled','2026-01-04'),
 (5,2,'paid','2026-01-05');
INSERT INTO order_items VALUES
 (1,1,'A',2,1000),(2,1,'B',1,500),(3,2,'A',1,1000),
 (4,3,'C',3,700),(5,4,'B',2,500),(6,5,'A',1,1000);
INSERT INTO payments VALUES
 (101,1,2500),(102,3,1000),(103,3,1100),(104,5,1000);
INSERT INTO refunds VALUES (201,3,700);
INSERT INTO inventory VALUES ('A',10),('B',10),('C',10);

Check the order-level grain

SELECT order_id, COUNT(*) AS item_rows,
       SUM(quantity * unit_price_cents) AS line_amount_cents
FROM order_items
GROUP BY order_id
ORDER BY order_id;

Worked records

Record 1

Order 1 has two items: two units at 1,000 cents and one at 500. Order 3 has three units at 700. Order 5 has one at 1,000. Orders 2 and 4 are unpaid and cancelled respectively.

Develop the reasoning

Order 1 line amount is 2×1,000+1×500=2,500 cents; order 3 is 2,100 and order 5 is 1,000. The three currently paid orders therefore have 5,600 cents of gross line amount in this fixture. All five orders together have 7,600 cents. Those totals use different eligible sets. Neither is automatically earned revenue or a period cash-flow measure. The grouped query returns one row per order, preserving the distinction between the two items in order 1 and the one order they belong to. A correct model makes the counting unit visible before a dashboard hides it behind a label.

Progressive practice — optional

1. Foundation

How many item rows belong to order 1, and how many orders do they represent?

Compare the worked reasoning

Two item rows represent one order.

Change the record

Another item is added to the same order.

Compare the changed-record reasoning

Item count rises while order count stays one; the metric must not confuse those grains.

2. Application

Is 7,600 cents the same metric as currently paid-order line amount?

Compare the worked reasoning

No. It includes unpaid and cancelled orders; the defined paid cohort is 5,600.

Change the record

Management asks about all created orders instead.

Compare the changed-record reasoning

That is a different contract and may use 7,600 in this limited fixture, with status and recognition limits stated.

3. Transfer

Can the fixture answer cash actually received during January?

Compare the worked reasoning

Not reliably, because payment dates are absent.

Change the record

Orders all have January creation dates.

Compare the changed-record reasoning

Order-cohort timing does not establish receipt timing. Add appropriate event-time data rather than relabel the existing total.

Sources and scope