ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2114Cartesian Product In R…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2114

    Database Systems

    Progress0 / 34 done
    Topics34
    1Basic Database Concepts2Database Approach vs File Based System3Database Architecture4Three Level Schema Architecture5Data Independence6Relational Data Model7Attributes8Schemas9Tuples10Domains11Relation Instances12Keys of Relations13Integrity Constraints14Relational Algebra15Selection in Relational Algebra16Projection in Relational Algebra17Cartesian Product in Relational Algebra18Types of Joins19Normalization20Functional Dependencies21Normal Forms22Entity-Relationship Model23Entity Sets24Attributes in Entity-Relationship Model25Relationship in Entity-Relationship Model26Entity-Relationship Diagrams27Structured Query Language (SQL)28Joins in SQL29Sub-Queries in SQL30Grouping and Aggregation in SQL31Concurrency Control32Database Backup and Recovery33Indexes34NoSQL Systems
    5 min read
    Topic 17 of 34
    Topic Study Note Illustration
    17

    Cartesian Product in Relational Algebra

    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...

    Cartesian Product in Relational Algebra

    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.

    Definition of Cartesian Product:

    • Notation: R × S
      • R and S: The two relations (tables) that are being combined.
      • The result of the Cartesian Product is a new relation that includes all combinations of tuples from R and S.

    Characteristics of Cartesian Product:

    • The attributes of the resulting relation are the union of the attributes of R and S.
    • If R has attributes A1, A2, ..., An and S has attributes B1, B2, ..., Bm, the resulting relation will have attributes A1, A2, ..., An, B1, B2, ..., Bm.
    • The number of rows in the result is the product of the number of rows in R and S. If R has r rows and S has s rows, then the result of R × S will have r * s rows.
    • The Cartesian Product does not filter or match any rows between the two relations. It simply pairs each row from R with every row from S.

    Example:

    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:

    • The first tuple from Students (Alice, 20) is combined with both tuples from Courses (C101, Database and C102, Mathematics).
    • The second tuple from 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).


    Cartesian Product and Joins:

    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).

    Example of Cartesian Product followed by Selection:

    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.


    Key Points about Cartesian Product:

    • Purpose: The Cartesian Product combines every tuple from one relation with every tuple from another, producing all possible pairwise combinations.
    • Result: The resulting relation has the attributes from both relations and the number of rows is the product of the number of rows in the two relations.
    • Usage: The Cartesian Product is often used as a step in performing a join, where further filtering (via selection) is applied.
    • Notation: R × S where R and S are the two relations involved.

    Limitations:

    • The Cartesian Product can result in large intermediate results, especially if the relations involved are large.
    • Without further filtering (like in a selection operation), the Cartesian Product can produce a lot of data that might not be meaningful or useful.

    Example in Context:

    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.


    Summary of Cartesian Product (×) in Relational Algebra:

    • Purpose: The Cartesian Product operation creates all possible pairs of tuples from two relations.
    • Syntax: R × S where R and S are the two relations.
    • Result: The resulting relation has all attributes from both relations and the number of rows is the product of the number of rows in each relation.
    • Use in Joins: The Cartesian Product can be used as a base for performing joins, where selection filters are applied afterward to match rows based on conditions.
    • Example: Students × Courses produces all combinations of students and courses.
    ← Previous topic 16
    Projection in Relational Algebra
    Next topic 18 →
    Types of Joins

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    5mremaining
    864words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Database Systems