Skip to content

Informatics Practices · Ch 1 — Querying and SQL Functions

Cartesian Product

1.4.4

Cartesian Product

The Cartesian product is a set operation that combines every row from one relation with every row from another relation. It does not care about matching values on common attributes — it simply pairs each row of the first table with each row of the second table. The symbol used for Cartesian product is ‘X’.

When you apply a Cartesian product, the resulting relation has a degree (number of columns) equal to the sum of the degrees of the two input relations. Its cardinality (number of rows) is the product of the cardinalities of the two input relations.

For example, consider two relations: DANCE and MUSIC. Both have degree 3. DANCE has 4 rows, and MUSIC has 5 rows. The Cartesian product DANCE X MUSIC will therefore produce a relation of degree 6 (3 + 3) and cardinality 20 (4 × 5).

The output shown in the textbook (Table 1.15) lists all 20 combinations. Each row of DANCE is paired with every row of MUSIC. Notice that the column names repeat — the first three columns come from DANCE (SNo, Name, Class) and the next three from MUSIC (SNo, Name, Class). Even though both tables have columns with the same names, the Cartesian product simply places them side by side.

Note

The Cartesian product is also called the cross product or cross join. In SQL, it is written as SELECT * FROM DANCE, MUSIC or SELECT * FROM DANCE CROSS JOIN MUSIC.

A key point to remember: the Cartesian product does not filter rows based on any condition. Every possible pair appears. This means if the first table has 4 rows and the second has 5 rows, you will always get 20 rows in the result, regardless of whether any values match. …

Table 1.15DANCE X MUSIC
SNoNameClassSNoNameClass
1Aastha7A1Mehak8A
2Mahira6A1Mehak8A
3Mohit7B1Mehak8A
4Sanjay7A1Mehak8A
1Aastha7A2Mahira6A
2Mahira6A2Mahira6A
3Mohit7B2Mahira6A
4Sanjay7A2Mahira6A
1Aastha7A3Lavanya7A
2Mahira6A3Lavanya7A
3Mohit7B3Lavanya7A
4Sanjay7A3Lavanya7A
1Aastha7A4Sanjay7A
2Mahira6A4Sanjay7A
3Mohit7B4Sanjay7A
4Sanjay7A4Sanjay7A
1Aastha7A5Abhay8A