Cartesian Product (×) is an operation in relational algebra that combines every tuple (row) from one relation with every tuple from another relation, creating all possible pairs of tuples. The result of a Cartesian Product is a new relation with a set of attributes derived from both relations, where the number of rows in the result is the product of the number of rows in the two relations involved.
R × S
A1, A2, ..., An and S has attributes B1, B2, ..., Bm, the resulting relation will have attributes A1, A2, ..., An, B1, B2, ..., Bm.r rows and S has s rows, then the result of R × S will have r * s rows.Consider the following two relations:
Students Relation:| StudentID | Name | Age |
|---|---|---|
| 1 | Alice | 20 |
| 2 | Bob | 22 |
Courses Relation:| CourseID | CourseName |
|---|---|
| C101 | Database |
| C102 | Mathematics |
Now, if we perform the Cartesian Product Students × Courses, we combine every row from Students with every row from Courses. This operation is written as:
Students × Courses
The result will be:
| StudentID | Name | Age | CourseID | CourseName |
|---|---|---|---|---|
| 1 | Alice | 20 | C101 | Database |
| 1 | Alice | 20 | C102 | Mathematics |
| 2 | Bob | 22 | C101 | Database |
| 2 | Bob | 22 | C102 | Mathematics |
In this case:
Students (Alice, 20) is combined with both tuples from Courses (C101, Database and C102, Mathematics).Students (Bob, 22) is also combined with both tuples from Courses.The total number of tuples in the result is 4 (2 rows from Students × 2 rows from Courses).
While the Cartesian Product creates all possible combinations of tuples from two relations, it is often used in combination with selection (σ) to create more meaningful results, especially for performing a join.
For example, in SQL, a natural join or an equijoin is essentially a Cartesian Product followed by a selection to match specific attributes between the two relations (e.g., matching on a StudentID).
Let’s assume the Students relation has a CourseID attribute (which was omitted in our example for simplicity). If we want to perform a join based on StudentID, we would first compute the Cartesian Product of Students and Courses, and then use selection to filter out combinations where the StudentID does not match in both relations.
σ(StudentID = StudentID)(Students × Courses)
This would result in only those rows where the StudentID from both relations matches.
R × S where R and S are the two relations involved.Consider an example where we want to combine Employees and Departments:
Employees Relation:| EmployeeID | Name |
|---|---|
| 1 | Alice |
| 2 | Bob |
Departments Relation:| DepartmentID | DepartmentName |
|---|---|
| D1 | HR |
| D2 | IT |
If we perform the Cartesian Product:
Employees × Departments
The result will be:
| EmployeeID | Name | DepartmentID | DepartmentName |
|---|---|---|---|
| 1 | Alice | D1 | HR |
| 1 | Alice | D2 | IT |
| 2 | Bob | D1 | HR |
| 2 | Bob | D2 | IT |
In this case, every employee is paired with every department. This may not be useful without further filtering or matching based on relevant conditions.
R × S where R and S are the two relations.Students × Courses produces all combinations of students and courses.Open this section to load past papers