Faragiri

Learn Joins SQL

The same two small tables joined four ways with the counts measured, the condition that turns a left join back into an inner one, and the total that is too high.

4Sections
3Min read
4Depth

Recommended Before This

0 / 2
  • SQL
  • SELECT

Four makers, four kilns, and the join decides how many rows come back.
Measured on MySQL 8.4 on 2026-09-02 against those two tables: an inner join
returns 3 rows, a left join 5, a right join 4, and a cross join 16. Same data,
four answers, and only one of them is what most questions mean.

The counts explain themselves once the data is visible. One maker has two
kilns, one has one, two have none, and one kiln has no maker. The inner join
keeps only rows that matched: three. The left join keeps every maker, so the two
with no kiln appear once each with NULL in the kiln columns: five. The right
join keeps every kiln, including the one with no maker: four.

What a left join actually produces

The rows are the thing to look at rather than the count:

Four makers, four kilns

Which region each join keeps

The overlap is the only region an inner join returns, and every other join is a decision about which of the two outer regions to keep as well.

  • Makers with no kiln
  • Kilns with no maker
3 rows that matched
  • Makers with no kilnBehrouz and Kaveh. A left join keeps them, with NULLs
  • Kilns with no makerThe spare. A right join keeps it, with NULLs

The figure cannot draw the row multiplication, which is the other half: Nasim has two kilns, so the overlap contains three rows rather than two makers, and that is where a total goes wrong.

Interactive Practice Lab

Edit and run this snippet directly in your browser (zero setup)

SQL
CREATE TABLE workshop_orders (id INTEGER PRIMARY KEY, supplier TEXT, qty INTEGER, city TEXT);INSERT INTO workshop_orders VALUES (1, 'Sepehr', 15, 'Ardabil'), (2, 'Aria', 47, 'Tabriz'), (3, 'Sepehr', 22, 'Ardabil'), (4, 'Nima', 31, 'Tehran');SELECT supplier, COUNT(*) AS deliveries, SUM(qty) AS total_qtyFROM workshop_ordersGROUP BY supplierORDER BY total_qty DESC;

Live Big-O Complexity & Binary Search Race Lab

Compare complexity curves at scale and watch Linear vs Binary Search race live.

  • Nasim | north and Nasim | south, so one maker
    appears twice
  • Behrouz | NULL, which is the row an inner join would have
    dropped
  • Roya | gas
  • Kaveh | NULL

Both properties matter. A join can multiply rows, so a maker with two kilns
is two rows and any sum over that result double counts anything from the maker's
side. And the unmatched rows arrive with NULLs, which is the whole point of the
left join and the reason the next section exists.

The two mistakes, and both are silent

The first is putting a condition on the right hand table in WHERE rather than
in ON. LEFT JOIN fg_kilns k ON k.maker_id = m.id WHERE k.litres > 100
turns the left join back into an inner join, because the NULL rows fail the WHERE
test. If the condition belongs to the join, it goes in ON.

Three quiet SQL traps

Join mistakes that return valid-looking wrong rows

None of these mistakes raises a syntax error; each silently alters which rows survive or how many times they pair.

Silent join bugs

  • Filter on right table placed in WHEREk.litres > 100 in WHERE rejects NULL rows
    • Turns left join into an inner join
    • Move join-specific filters into ON
  • Missing ON clausePairs every row against every row
    • Produces 16 rows here, far more on real tables
    • Symptom is an oversized result or a hung query
  • Self join using <> instead of <Joins fg_makers to itself on same town
    • <> lists every pair twice in reverse
    • Use a.id < b.id to remove mirrored duplicates

Check row counts early whenever a join adds or drops conditions; silent errors rarely fail loudly.

The second is a missing ON clause entirely, which in most engines produces a
cross join: every row against every row, 16 here and a hundred million on real
tables. The tell is a query that returns far too many rows or never finishes, and
it is one missing line.

A self join is the same table twice with two aliases, and it is the shape
people find surprising until they see it once: FROM fg_makers a JOIN
fg_makers b ON a.town = b.town AND a.id < b.id
lists pairs of makers in
the same town without listing each pair twice. The < rather than
<> is what removes the mirrored duplicate, and it is the
detail that is always wrong the first time.

What to run in 2026

EXPLAIN is the tool, free with every engine, and on a join it
reports the order the tables are read and whether each side uses an index. A
join with no index on the foreign key is the most common cause of a slow one,
and the plan says so directly. It replaced guessing at which table is the
problem.

What to run in 2026

Two habits that keep multi-table queries fast and explicit

Reading the join plan alongside explicit JOIN syntax prevents both missing foreign-key indexes and accidental cross joins.

  • EXPLAIN on every join
  • Explicit JOIN ... ON syntax
  • EXPLAIN on every joinShows table read order and flags joins missing an index on the foreign key
  • Explicit JOIN ... ON syntaxReplaces comma-separated FROM lists so a forgotten condition cannot hide in WHERE

Comma joins still parse everywhere, so knowing how to read them helps when maintaining older SQL while writing explicit JOINs yourself.

Explicit JOIN syntax is the second, free and standard, and it
replaced listing tables in FROM with the conditions in WHERE. That older form
still works everywhere and is worth being able to read, and the reason not to
write it is that a forgotten condition becomes a cross join with nothing
visibly missing.

Where this stalls people

The stall is a total that is too high, from a report nobody can find the bug
in. It is nearly always a join multiplying rows: a maker joined to two kilns
appears twice, and summing the maker's own figures over that result counts them
twice.

Where joins stall people

How to catch and fix a join that doubles totals

When a summary report returns numbers that are too high, row multiplication from a one-to-many join is almost always the cause.

  1. 01Spot the inflated aggregateA maker with two kilns appears twice before SUM runs
  2. 02Drop the aggregate temporarilyRun the same query without SUM or COUNT to see raw rows
  3. 03Count the returned rowsIf rows exceed the entities you are reporting on, the join is duplicating
  4. 04Aggregate the joined side firstSummarise child rows before joining instead of patching the outer total

Never divide or tweak an inflated total at the end; fix the row granularity before the outer join runs.

The check is to run the query without the aggregate and count the rows.
If the row count is higher than the number of things you are reporting on, the
join is duplicating, and the fix is to aggregate the joined side first rather
than to adjust the total. Choosing rows and columns is
choosing rows and columns,
what to do after the join is
counting, summing and
grouping by
, and what the keys being joined on promise is
primary keys,
foreign keys and what they promise
, inside
the language for asking a database
questions
.

Alongside this

Above this