Computer Science · Ch 9 — Structured Query Language (SQL)
Cartesian Product (X)
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 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 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: columns.
- Cardinality: rows. …
| SNo | Name | Class | SNo | Name | Class |
|---|---|---|---|---|---|
| 1 | Aastha | 7A | 1 | Mehak | 8A |
| 2 | Mahira | 6A | 1 | Mehak | 8A |
| 3 | Mohit | 7B | 1 | Mehak | 8A |
| 4 | Sanjay | 7A | 1 | Mehak | 8A |
| 1 | Aastha | 7A | 2 | Mahira | 6A |
| 2 | Mahira | 6A | 2 | Mahira | 6A |
| 3 | Mohit | 7B | 2 | Mahira | 6A |
| 4 | Sanjay | 7A | 2 | Mahira | 6A |
| 1 | Aastha | 7A | 3 | Lavanya | 7A |
| 2 | Mahira | 6A | 3 | Lavanya | 7A |
| 3 | Mohit | 7B | 3 | Lavanya | 7A |
| 4 | Sanjay | 7A | 3 | Lavanya | 7A |
| 1 | Aastha | 7A | 4 | Sanjay | 7A |
| 2 | Mahira | 6A | 4 | Sanjay | 7A |
| 3 | Mohit | 7B | 4 | Sanjay | 7A |
| 4 | Sanjay | 7A | 4 | Sanjay | 7A |