{
  "id": 9899438,
  "title": "Why Your JOIN Doubled Your Totals — There's No Error, Just the Wrong Number",
  "url": "https://urgent.news/2026/09/26/why-your-join-doubled-your-totals-theres-no-error-just-the-wrong",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-26T03:27:04.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/systemcraftdev/why-your-join-doubled-your-totals-theres-no-error-just-the-wrong-number-5a3i"
  },
  "original_language": "en",
  "account": "When you execute a SQL query that joins multiple tables and aggregates the results with SUM() or COUNT(), it's easy to overlook the fact that the JOIN operation can multiply the number of rows unexpectedly. This multiplication, known as a \"fan-out,\" often leads to inflated totals that look correct but are actually incorrect. The issue arises because each row from one table is matched against multiple rows from another table based on the join condition, resulting in duplicated rows. This duplication is accurate according to SQL rules, but it can skew the final sum or count. To avoid this error, first check the row count before relying on the total result. If the row count is off, investigate which table is causing the fan-out by joining. Identify the one-to-many relationship in your joins, as that's often the source of the problem. Refactor your query to aggregate the one-to-many table's data separately in a subquery before joining it back, ensuring the raw data is reduced to one row per key before it's aggregated. Avoid using DISTINCT in place of this approach, as it can discard legitimate duplicate values. Remember, the issue lies in the number of rows produced by the join, not in the aggregate function itself. By verifying row counts and restructuring your joins to aggregate early, you can prevent silently incorrect totals caused by unexpected fan-out in your SQL queries.",
  "summary": "SQL doesn't warn you when a JOIN multiplies your rows — it just quietly hands back more of them than you expected, and every SUM() and COUNT() downstream inherits the mistake. Here's the exact mechanism, and how to catch it before it ships. Adapted from the SQL Essentials Companion Guide . You write a query to total up each customer's orders, and it runs fine — no error, no red text, just a…",
  "key_points": [],
  "editors_take": "Understanding that JOIN operations can multiply rows and inflate totals helps developers identify and fix errors by restructuring queries to aggregate data earlier and verify row counts accurately.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}