Skip to content

Computer Science · Ch 9 — Structured Query Language (SQL)

Cartesian Product (X)

9.10.4

Cartesian Product (X)

The Cartesian product (also called the cross product) is a relational algebra operation that combines every row from one table with every row from another table. It is denoted by the symbol X.

Unlike the join operations you will study later, the Cartesian product does not check for matching values on common attributes. It simply pairs each row of the first relation with each row of the second relation, regardless of whether the data in those rows is related or not.

Effect on Degree and Cardinality

When you apply the Cartesian product to two relations, the structure and size of the resulting relation change in a predictable way:

  • Degree (number of columns): The degree of the result is the sum of the degrees of the two input relations. If one table has 3 columns and the other has 3 columns, the result will have 3+3=63 + 3 = 6 columns.
  • Cardinality (number of rows): The cardinality of the result is the product of the cardinalities of the two input relations. If one table has 4 rows and the other has 5 rows, the result will have 4×5=204 \times 5 = 20 rows.

Example with DANCE and MUSIC

The textbook uses two relations named DANCE and MUSIC to illustrate this. Both relations have a degree of 3 (three columns each: SNo, Name, Class). The DANCE relation has a cardinality of 4 rows, and the MUSIC relation has a cardinality of 5 rows.

Applying the Cartesian product DANCE X MUSIC produces a new relation with:

  • Degree: 3+3=63 + 3 = 6 columns.
  • Cardinality: 4×5=204 \times 5 = 20 rows. …
Table 9.23DANCE X MUSIC
SNoNameClassSNoNameClass
1Aastha7A1Mehak8A
2Mahira6A1Mehak8A
3Mohit7B1Mehak8A
4Sanjay7A1Mehak8A
1Aastha7A2Mahira6A
2Mahira6A2Mahira6A
3Mohit7B2Mahira6A
4Sanjay7A2Mahira6A
1Aastha7A3Lavanya7A
2Mahira6A3Lavanya7A
3Mohit7B3Lavanya7A
4Sanjay7A3Lavanya7A
1Aastha7A4Sanjay7A
2Mahira6A4Sanjay7A
3Mohit7B4Sanjay7A
4Sanjay7A4Sanjay7A