An SQL exercise with three orders, a broken join, and an answer key.
The fastest way to inflate an order total is to join it to two tables that each contain several rows for the same order. The result can look completely normal in a chart. In this example, the correct net revenue is $265. One plausible SQL query returns $690.
This exercise uses Julius AI as the conversational layer and SQLite as the independent check. The example data and query results below were verified in SQLite. The suggested Julius prompt is an exercise you can reproduce with the same inputs.
Disclosure: AI assisted with drafting. This article includes one affiliate link, and I may earn a commission if you subscribe through it.
Run the following in SQLite to create the fixture. An orders table has one row per order. The other two tables hold individual line items and refunds.
CREATE TABLE orders (
order_id TEXT PRIMARY KEY,
order_total INTEGER NOT NULL
);
CREATE TABLE items (
order_id TEXT NOT NULL,
sku TEXT NOT NULL,
line_total INTEGER NOT NULL
);
CREATE TABLE refunds (
order_id TEXT NOT NULL,
refund_id TEXT PRIMARY KEY,
refund_amount INTEGER NOT NULL
);
INSERT INTO orders VALUES ('A101', 120), ('A102', 80), ('A103', 100);
INSERT INTO items VALUES
('A101', 'X', 70), ('A101', 'Y', 50), ('A102', 'X', 80),
('A103', 'Y', 60), ('A103', 'Z', 40);
INSERT INTO refunds VALUES
('A101', 'R1', 10), ('A101', 'R2', 5), ('A103', 'R3', 20);
Export each table as a CSV named orders.csv, items.csv and refunds.csv. Upload the three files in Julius. The fixture uses whole dollars for readability. For real billing data, use a fixed-precision decimal type or store amounts as integer minor units and group by currency before summing.
SELECT COUNT(*) AS joined_rows,
SUM(o.order_total) AS gross,
SUM(COALESCE(r.refund_amount, 0)) AS refunds
FROM orders AS o
LEFT JOIN items AS i USING (order_id)
LEFT JOIN refunds AS r USING (order_id);
Order A101 has two items and two refunds. The join emits all four item–refund combinations, repeating the $120 order total four times. It also repeats each refund twice. A103 contributes two joined rows. The result is seven joined rows, $760 gross, $70 refunds and $690 net.
Putting DISTINCT inside the sum is unsafe. Two separate orders can have the same order total and two legitimate refunds can have the same amount. A unique dollar value is a different concept from a unique order or refund ID.
Since the order table already holds the order total, the revenue calculation only needs a refund subtotal per order. Leave the line-item table out of this calculation. If you need item counts, aggregate items separately by order ID and join that result.
WITH refunds_by_order AS (
SELECT order_id, SUM(refund_amount) AS refund_total
FROM refunds
GROUP BY order_id
)
SELECT COUNT(*) AS order_count,
SUM(o.order_total) AS gross,
SUM(COALESCE(r.refund_total, 0)) AS refunds,
SUM(o.order_total - COALESCE(r.refund_total, 0)) AS net
FROM orders AS o
LEFT JOIN refunds_by_order AS r USING (order_id);
Expected output: 3 orders, $300 gross, $35 refunded and $265 net. Confirm that every order ID comes out once. That catches duplicate joins even if equal and opposite errors happen to cancel in the totals.
Describe the row grain and key of each file. Show duplicate order IDs in orders and duplicate refund IDs in refunds. Report orphan refunds separately. Aggregate refunds to order ID before joining them to orders. Return the order count, gross, refunds and net, with an order-level reconciliation. Show the calculation and wait before charting.
The orphan check matters. A left join from orders will omit a refund whose order is missing from the export, even if the SQL is technically correct. Real investigations also need a rule for partial periods and the date a refund is recognized. The tiny fixture isolates the join so you can test it first.
Julius helps when you want to inspect an unfamiliar export or write a quick reconciliation. If the output drives finance reporting, keep the SQL and its assertions in version control and run them when a new export arrives.
Eligible new subscribers can get 50% off their first month. Try Julius AI through my affiliate link and use PRATHAM50OFFYJ if prompted for a code. Check the amount due before subscribing.
Product reference: Julius start guide.