Skip to content

Cartesian join: missing join condition

Quick summary

Your JOIN is missing an ON clause — the database returns every combination of rows from both tables (Cartesian product).

Why this happens

  • A JOIN statement is missing the ON clause that specifies the join condition

  • A CROSS JOIN was used unintentionally instead of an INNER JOIN with a condition

  • The ON clause references the wrong columns, resulting in a near-Cartesian product

Minimal example

✗ Broken code
await db.query('SELECT * FROM users JOIN orders');
// Missing ON clause — returns users.count * orders.count rows (Cartesian product!)
✓ Fixed code
await db.query('SELECT * FROM users JOIN orders ON users.id = orders.user_id');
// Each user matched with their orders only

Adding the ON clause with the correct join condition limits the result to matching rows instead of all possible combinations.

How to diagnose

  • Check the row count of the result — a Cartesian product has rows equal to table1.rows times table2.rows

  • Review the JOIN clause for a missing or incorrect ON condition

  • Add EXPLAIN to see if the query plan shows a nested loop without a filter

How to fix

  • Add the correct ON clause to specify the join condition: JOIN table2 ON table1.id = table2.t1_id

  • Replace CROSS JOIN with INNER JOIN and add the appropriate join condition

  • Verify the join columns are correct and have compatible types

How to prevent

  • Always include an ON clause for INNER, LEFT, RIGHT, and FULL joins

  • Check result row counts against expectations to detect accidental Cartesian products

Related Resources

Related Glossary

Related Lessons

  • Joins

    Learn about join conditions.

← Back to language