Why a Database Join Creates Extra Rows: A Laboratory-Results Example

A diagram comparing several ways to organize relationships in a database. A relational join depends on the keys and matching rules used, not just on the visual arrangement of rows.

A database query can return more rows than either of its input tables without making a mistake. A join produces combinations that satisfy its matching condition. If one sample has several test results, its identifier should appear several times in a result-level output. Trouble begins when a report expects one row per sample but uses a query whose rows represent individual tests, or combinations of tests and other records.

The example below uses invented laboratory data. The same reasoning applies to orders and order items, projects and invoices, or customers and appointments. Before removing apparent duplicates, identify what one row is supposed to represent.

State the unit of a row

Imagine a Samples table with two entries, S01 and S02. A Results table contains three entries: two tests for S01 and one test for S02. A join on the sample identifier returns three rows. S01 appears twice because it has two matching results. That repeated identifier is useful information, not proof that the sample was entered twice.

The PostgreSQL explanation of table expressions describes joins in terms of matching rows. For practical analysis, write the output’s intended grain above the query: “one row per sample” or “one row per sample and test.” This short description provides a target against which the result can be checked.

SampleTestResult identifier
S01Assay AR101
S01Assay BR102
S02Assay AR103

The table has three results and two samples. Both counts are correct. The error would be labelling the number of rows as the number of distinct samples without accounting for the relationship.

Two sample records connect to three result records, producing three joined rows because sample S01 has two results.
A one-to-many join repeats S01 legitimately. Count samples and result rows separately. Image credits.

Check the key on both sides

A join condition can look plausible while using an insufficient identifier. A sample label might be unique only within a project, laboratory or collection date. If two projects both use S01, joining on S01 alone can connect records that do not belong together. The correct relationship may require a project identifier as well as the local sample label.

Document the actual key rather than relying on the appearance of the data currently on screen. A small test file may happen to contain no repeated labels, masking a problem that appears when another batch is added. The question is whether the system guarantees uniqueness, not whether a quick glance finds a duplicate today.

Understand multiplication across two one-to-many tables

Now add a Notes table. S01 has two test results and three notes. Joining Samples to both tables using only the sample identifier can create six S01 rows: every matching test combines with every matching note. The query has not discovered six tests. It has constructed six combinations permitted by its conditions.

Sketch the combinations before changing the query. Two results multiplied by three notes gives six pairs. If the report needs a test list and a separate note list, two outputs may be clearer. If it needs one summary row per sample, aggregate each child table to that level before combining them. The solution follows from the desired meaning of a row.

Use constraints to express the intended structure

Primary keys, unique constraints and foreign keys make relationships explicit. The PostgreSQL constraints documentation describes these mechanisms. A result identifier can uniquely identify one result, while a foreign key connects it to an existing sample. A separate uniqueness rule may be appropriate for a combination such as sample, assay and replicate number, depending on the actual workflow.

Do not invent a uniqueness rule merely to make a report shorter. Repeated measurements, reruns and different instruments can legitimately generate several results for the same sample and assay. The database should preserve those distinctions. If only one result is selected for a summary, record the selection rule instead of deleting the other observations.

Investigate before applying DISTINCT

A command that removes identical output rows can hide a faulty join without repairing its logic. It can also discard information if two different records happen to display the same chosen columns. Two measurements with the same value remain two measurements when they have separate result identifiers or collection events.

Temporarily include the keys from every table in the output. Inspect one sample with extra rows. Identify exactly which result and note produced each combination. This diagnostic view is often more revealing than counting the final report alone. After the relationship is understood, remove diagnostic columns if they are not needed for the audience.

For a summary, decide whether you need a count, the latest eligible result, a mean of specified replicates, or a list of observations. These are different operations. “One row” is a formatting requirement; it does not specify which scientific or business meaning the row should carry.

Preserve unmatched records deliberately

An inner join returns matching combinations. A left join also preserves rows from its left side that have no match, filling the other side with nulls. This difference matters when a sample exists but a test is pending. A report built with an inner join can make the untested sample disappear from view, even though it remains in the database.

Choose the behaviour according to the report’s purpose. A completed-results report may intentionally exclude pending samples. A workflow dashboard usually needs to show them. Document that choice in the report title or accompanying definition so that a reader does not confuse “not listed” with “never collected.”

Check a total before and after the join

Suppose the two S01 results each have a recorded processing cost. Joining them to three notes repeats each cost three times. Summing the joined cost column would therefore triple the sample’s total even though no new test was performed. This is an aggregation error caused by the output grain.

Compare the total from the result table with the total after joining. If the report needs notes alongside a cost summary, summarise the costs at the intended level first and join a separately prepared note summary. The arithmetic then follows the meaning of the records.

Write a small reconciliation check

For the invented example, record three numbers: two sample records, three result records and three sample-result rows. After adding three notes for S01, predict the combinations before running the combined query. A mismatch between prediction and output points toward a key, filter or relationship that deserves inspection.

This habit becomes valuable when the data concern biological samples or peptide assays. A clean table can still be wrong about its unit of analysis. Naming the row, checking the key and explaining the relationship make a result easier to trust long before any statistical comparison is attempted.