0PricingLogin
SQL Academy · Lesson

Cross Joins and Cartesian Products

Use CROSS JOIN deliberately for combinatorial pairings, and avoid accidental Cartesian explosions from missing join keys.

What Is a Cross Join?

A CROSS JOIN produces every combination of rows: m × n output rows. No ON clause.

SELECT * FROM a CROSS JOIN b;

-- Equivalent:
SELECT * FROM a, b;        -- comma join, no condition

Accidental Cartesian Products

The classic bug: forget the JOIN condition, get every row of A paired with every row of B.

SELECT u.email, o.id
FROM users u, orders o;     -- missing WHERE u.id = o.user_id !
-- m users × n orders rows — millions!

All lessons in this course

  1. Cross Joins and Cartesian Products
  2. Lateral Joins (LATERAL JOIN)
  3. Anti-Joins and Semi-Joins (NOT EXISTS)
  4. Multi-Table Join Performance Tuning
← Back to SQL Academy