{"slug": "catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue", "title": "Catch a many-to-many join before Julius AI charts the wrong revenue", "summary": "A developer published a reproducible SQL exercise showing how a many-to-many join between orders, line items and refunds inflates net revenue from a correct $265 to $690 in Julius AI, a conversational analytics tool. The fixture, verified in SQLite, demonstrates that joining the orders table to both items and refunds multiplies rows, and that pre-aggregating refunds by order ID before joining yields the correct 3 orders, $300 gross, $35 refunded and $265 net.", "body_md": "*An SQL exercise with three orders, a broken join, and an answer key.*\n\nThe 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**.\n\nThis 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.\n\n*Disclosure: AI assisted with drafting. This article includes one affiliate link, and I may earn a commission if you subscribe through it.*\n\nRun 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.\n\n```\nCREATE TABLE orders (\n  order_id TEXT PRIMARY KEY,\n  order_total INTEGER NOT NULL\n);\n\nCREATE TABLE items (\n  order_id TEXT NOT NULL,\n  sku TEXT NOT NULL,\n  line_total INTEGER NOT NULL\n);\n\nCREATE TABLE refunds (\n  order_id TEXT NOT NULL,\n  refund_id TEXT PRIMARY KEY,\n  refund_amount INTEGER NOT NULL\n);\n\nINSERT INTO orders VALUES ('A101', 120), ('A102', 80), ('A103', 100);\nINSERT INTO items VALUES\n  ('A101', 'X', 70), ('A101', 'Y', 50), ('A102', 'X', 80),\n  ('A103', 'Y', 60), ('A103', 'Z', 40);\nINSERT INTO refunds VALUES\n  ('A101', 'R1', 10), ('A101', 'R2', 5), ('A103', 'R3', 20);\n```\n\nExport 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.\n\n```\nSELECT COUNT(*) AS joined_rows,\n       SUM(o.order_total) AS gross,\n       SUM(COALESCE(r.refund_amount, 0)) AS refunds\nFROM orders AS o\nLEFT JOIN items AS i USING (order_id)\nLEFT JOIN refunds AS r USING (order_id);\n```\n\nOrder 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.\n\nPutting `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.\n\nSince 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.\n\n```\nWITH refunds_by_order AS (\n  SELECT order_id, SUM(refund_amount) AS refund_total\n  FROM refunds\n  GROUP BY order_id\n)\nSELECT COUNT(*) AS order_count,\n       SUM(o.order_total) AS gross,\n       SUM(COALESCE(r.refund_total, 0)) AS refunds,\n       SUM(o.order_total - COALESCE(r.refund_total, 0)) AS net\nFROM orders AS o\nLEFT JOIN refunds_by_order AS r USING (order_id);\n```\n\nExpected 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.\n\nDescribe 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.\n\nThe 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.\n\nJulius 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.\n\nEligible new subscribers can get **50% off their first month**. [Try Julius AI through my affiliate link](https://julius.dub.link/pratham-malhotra) and use **PRATHAM50OFFYJ** if prompted for a code. Check the amount due before subscribing.\n\n*Product reference: [Julius start guide](https://julius.ai/faq/chat-start-guide).*", "url": "https://wpnews.pro/news/catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue", "canonical_source": "https://dev.to/prathammalhotra/catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue-35ja", "published_at": "2026-11-25 11:27:39+00:00", "updated_at": "2026-11-25 11:43:08.659194+00:00", "lang": "en", "topics": ["ai-tools", "ai-products"], "entities": ["Julius AI", "SQLite"], "also_reported_by": [], "takeaway": "Aggregate child tables like refunds to the order ID with GROUP BY before joining, and verify the order count matches the orders table, since a direct join to both items and refunds inflated net revenue from $265 to $690.", "recommendation": {"advice": "Pre-aggregate one-to-many tables to their parent key before joining, and check that every order ID appears exactly once to catch duplicate joins before charting results.", "playbook": "output-verification", "playbook_url": "https://wpnews.pro/playbooks/output-verification"}, "alternates": {"html": "https://wpnews.pro/news/catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue", "markdown": "https://wpnews.pro/news/catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue.md", "text": "https://wpnews.pro/news/catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue.txt", "jsonld": "https://wpnews.pro/news/catch-a-many-to-many-join-before-julius-ai-charts-the-wrong-revenue.jsonld"}}