ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Database Systems
    CC-215
    Progress0 / 34 topics
    Topics
    1. Basic Database Concepts2. Database Approach vs File Based System3. Database Architecture4. Three Level Schema Architecture5. Data Independence6. Relational Data Model7. Attributes8. Schemas9. Tuples10. Domains11. Relation Instances12. Keys of Relations13. Integrity Constraints14. Relational Algebra15. Selection in Relational Algebra16. Projection in Relational Algebra17. Cartesian Product in Relational Algebra18. Types of Joins19. Normalization20. Functional Dependencies21. Normal Forms22. Entity-Relationship Model23. Entity Sets24. Attributes in Entity-Relationship Model25. Relationship in Entity-Relationship Model26. Entity-Relationship Diagrams27. Structured Query Language (SQL)28. Joins in SQL29. Sub-Queries in SQL30. Grouping and Aggregation in SQL31. Concurrency Control32. Database Backup and Recovery33. Indexes34. NoSQL Systems
    CC-215›Cartesian Product in Relational Algebra
    Database SystemsTopic 17 of 34

    Cartesian Product in Relational Algebra

    5 minread
    864words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time5 min
      Word count864
      Code examples0
      DifficultyBeginner