Informatics Practices · Ch 1 — Querying and SQL Functions
Cartesian Product
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.
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. …
| 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 |
| 1 | Aastha | 7A | 5 | Abhay | 8A |