Beginner

575. Cartesian Product

Understand the Cartesian product created when every row from one table is paired with every row from another table.

📘 Lesson 575 of 728 ⏱ 12 min 🎯 Intermediate

A Cartesian product contains every possible combination of rows from two datasets. In SQL, it is commonly produced using CROSS JOIN or an improperly restricted JOIN.

Unexpected Cartesian products can cause massive result sets and incorrect calculations.

If Products contains 10 rows and Stores contains 5 rows, a CROSS JOIN produces 50 combinations.

SQL
SELECT
    p.ProductName,
    s.StoreName
FROM Products p
CROSS JOIN Stores s;

Useful for controlled combination generation but important to recognize as a potential SQL error.

  • Cartesian product creates all combinations.
  • CROSS JOIN intentionally creates this result.
  • Missing JOIN conditions can accidentally create it.
  • Large tables can produce huge results.
  • Always verify JOIN conditions.
What is a Cartesian product?

It is a result containing every possible combination of rows from two datasets.

How can an accidental Cartesian product occur?

It can occur when tables are combined without a proper JOIN condition.

Have you finished this lesson?

Introduction to SQL – All Lessons