A SQL join inflated the total: count records before matches

Three award records add to 300 units. Attach their source references with a SQL join and the total becomes 420. No award amount changed. One record appeared twice because it had two matching references.

Before reporting a combined dataset’s total, define what one row represents. A row for an award and a row for an award–source match are different units of counting. Removing repeated-looking amounts can create another error, because two genuine records may have the same amount.

Three teal record blocks connected to different numbers of ivory source markers
ImageGen conceptual illustration of records and references; the connections are not an observed research dataset.

Make the repeated match visible

This is an invented teaching example, not a finding about actual spending. In a new, empty SQLite database, run the following SQL. Table awards has one row per award ID; table sources has one row per reference. A has two references, B has one, and C has none in this table.

CREATE TABLE awards(award_id TEXT PRIMARY KEY, amount INTEGER);
CREATE TABLE sources(source_id TEXT PRIMARY KEY, award_id TEXT);
INSERT INTO awards VALUES ('A',120),('B',120),('C',60);
INSERT INTO sources VALUES ('s1','A'),('s2','A'),('s3','B');

SELECT a.award_id, a.amount, s.source_id
FROM awards AS a
LEFT JOIN sources AS s ON s.award_id = a.award_id
ORDER BY a.award_id, s.source_id;
Award IDAmountMatched reference
A120s1
A120s2
B120s3
C60NULL

The first two rows are different matches for the same award. They are not evidence of two awards of 120. SQLite’s SELECT documentation describes how matching rows are combined; a LEFT JOIN also retains an unmatched left-hand record with NULL on the right.

We executed this synthetic example locally with SQLite 3.49.1 through Python 3.12.10. It produced the four rows above. The original awards table contained three records with a sum of 300; the joined amounts summed to 420. This checks the query’s behavior on this fixture, not the completeness of a real research dataset.

Five calculations, five different meanings

SELECT COUNT(*) AS joined_rows,
       COUNT(s.source_id) AS source_rows,
       COUNT(DISTINCT a.award_id) AS award_records,
       SUM(a.amount) AS repeated_total,
       SUM(DISTINCT a.amount) AS unique_amount_total
FROM awards AS a
LEFT JOIN sources AS s ON s.award_id = a.award_id;

The output is 4, 3, 3, 420, 180, in the order of the selected columns. Four counts all joined rows, including C’s unmatched row. The source_rows result of 3 counts non-NULL reference IDs. The award_records result of 3 counts distinct award IDs. Neither number tells you whether the references are independent sources.

The 420 repeats A’s amount. The 180 comes from adding each distinct amount value: 120 + 60. It incorrectly collapses A and B, even though their IDs differ. SQLite’s aggregate reference explains that DISTINCT filters values supplied to the aggregate. It does not know which business entity the value belongs to.

A plain SELECT DISTINCT on the detailed three-column result does not help: A/s1 and A/s2 are distinct rows already. Dropping the reference column to hide the difference would also remove information you need to explain the join. Diagnose the repeated key before deciding which records may be combined.

Keep each award once and retain the reference count

For the question “What is the total amount across these award records?”, start from one row per award ID. If you also need the number of references, aggregate the reference table to that same level before joining:

WITH source_counts AS (
  SELECT award_id, COUNT(*) AS source_rows
  FROM sources
  GROUP BY award_id
)
SELECT a.award_id, a.amount,
       COALESCE(s.source_rows, 0) AS source_rows
FROM awards AS a
LEFT JOIN source_counts AS s ON s.award_id = a.award_id
ORDER BY a.award_id;

The result is A/120/2, B/120/1 and C/60/0. Its amount column totals 300. Here COALESCE turns the missing matched reference count into zero because the reference table has no row for C. That zero does not mean C has no evidence anywhere on the web, nor should this conversion be applied to an unknown award amount.

Changing LEFT JOIN to INNER JOIN would discard C. That may suit a question specifically about records with a matched reference, but it changes the population. Write that restriction beside the total. A filter on the right-hand table can change the included population too; inspect retained IDs after applying it.

A short reconciliation to save with the result

Record the source files and their versions, the intended unit of counting, the join keys, the expected matches per key, the SQL, and the output IDs. Compare the parent-record count and amount before and after each join. Investigate keys with several matches and parents with none. If the parent key is not unique, resolve whether rows represent revisions, line items or erroneous duplicates before summing.

A damaged identifier can create a different matching problem; use the CSV import checks before joining an exported file. Preserve the acquisition context of the database snapshot too; this exercise does not test whether a received database is complete.

For this exercise, the defensible note is: “Three award records total 300 units. Their LEFT JOIN to three reference records produces four rows and a repeated amount total of 420.” Put both the counting unit and the query into the investigation report template so a reviewer can reproduce the distinction.

Author: OSINT Jet · Primary sources reviewed 1 December 2026