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›Relational Algebra
    Database SystemsTopic 14 of 34

    Relational Algebra

    7 minread
    1,124words
    Intermediatelevel

    Relational Algebra in Databases

    Relational Algebra is a formal system for manipulating and querying data in a relational database. It provides a set of operations that can be performed on relations (tables) to retrieve and modify data. These operations are the theoretical foundation of SQL and enable us to express complex queries in a concise and systematic manner. Relational algebra consists of a set of basic operations, which can be combined to perform more complex queries.

    Key Operations in Relational Algebra

    1. Selection (σ):

      • The selection operation is used to filter rows (tuples) based on a specified condition. It extracts tuples that satisfy a given predicate.
      • Notation: σ_condition(R)
        • R: The relation (table) to apply the operation to.
        • condition: A logical condition to filter the rows.
      • Result: A subset of the relation that satisfies the condition.

      Example: Suppose we have a Students table and want to select all students whose age is greater than 21:

      σ(Age > 21)(Students)
      

      This operation will return all rows from the Students table where the Age is greater than 21.

    2. Projection (π):

      • The projection operation is used to select specific columns (attributes) from a relation, removing duplicates.
      • Notation: π_column1, column2, ... (R)
        • R: The relation from which to select attributes.
        • column1, column2, ...: The attributes to be included in the result.
      • Result: A relation with only the specified columns, and duplicates removed.

      Example: If we want to retrieve just the names and ages of students from the Students table:

      π(Name, Age)(Students)
      

      This operation will return a relation with only the Name and Age columns from the Students table.

    3. Union (∪):

      • The union operation combines the tuples of two relations that have the same set of attributes. It returns all distinct tuples from both relations.
      • Notation: R1 ∪ R2
        • R1 and R2: Two relations to be combined.
      • Result: A relation containing all distinct tuples from both R1 and R2.

      Example: If we have two relations, StudentsInCS and StudentsInMath, and want to find all distinct students enrolled in either of the two courses:

      StudentsInCS ∪ StudentsInMath
      

      This operation returns a relation with all distinct students from both the StudentsInCS and StudentsInMath relations.

    4. Set Difference (−):

      • The set difference operation finds the tuples that are present in one relation but not in another. It returns tuples from the first relation that do not exist in the second.
      • Notation: R1 − R2
        • R1 and R2: The two relations to compare.
      • Result: A relation containing tuples from R1 that are not in R2.

      Example: If we want to find students who are enrolled in StudentsInCS but not in StudentsInMath:

      StudentsInCS − StudentsInMath
      

      This operation will return the students who are in the StudentsInCS relation but not in the StudentsInMath relation.

    5. Intersection (∩):

      • The intersection operation returns the set of tuples that are common to both relations.
      • Notation: R1 ∩ R2
        • R1 and R2: The two relations to be compared.
      • Result: A relation containing tuples that exist in both R1 and R2.

      Example: If we want to find students who are enrolled in both StudentsInCS and StudentsInMath:

      StudentsInCS ∩ StudentsInMath
      

      This operation will return the students who are enrolled in both courses.

    6. Cartesian Product (×):

      • The Cartesian product operation returns a relation that consists of every possible combination of tuples from two relations. The result is the combination of all attributes from both relations.
      • Notation: R1 × R2
        • R1 and R2: The two relations whose Cartesian product is to be computed.
      • Result: A relation containing all combinations of tuples from R1 and R2.

      Example: If Students contains information about students and Courses contains a list of courses, we can find all possible combinations of students and courses:

      Students × Courses
      

      This operation returns a relation where each tuple represents a combination of a student and a course.

    7. Rename (ρ):

      • The rename operation is used to rename the attributes of a relation. It is particularly useful when performing operations like joins or when we need to make the attribute names more descriptive.
      • Notation: ρ(new_name1, new_name2, ...)(R)
        • new_name1, new_name2, ...: The new names for the attributes.
        • R: The relation whose attributes are to be renamed.
      • Result: A relation with the renamed attributes.

      Example: If we have a Students table with attributes StudentID and StudentName, and we want to rename them to ID and Name:

      ρ(ID, Name)(Students)
      

      This operation returns a relation with the ID and Name attributes, instead of StudentID and StudentName.


    Advanced Operations

    These basic operations can be combined and extended to express more complex queries. There are also a few additional relational algebra operations that are important to know:

    1. Join (⨝):

      • The join operation combines tuples from two relations based on a common attribute (or set of attributes). It is a combination of the Cartesian product followed by selection (σ).
      • Notation: R1 ⨝ R2
      • There are different types of joins:
        • Equi-join: A join where the condition is based on equality between attributes.
        • Natural join: A join where common attributes with the same name are automatically matched.

      Example: If we want to join the Students table with the Courses table on StudentID:

      Students ⨝ Courses
      
    2. Division (÷):

      • The division operation is used when we want to find tuples in one relation that are related to all tuples in another relation. It's useful in queries like "find all students who have taken every course".
      • Notation: R1 ÷ R2
        • R1: The relation whose tuples we are interested in.
        • R2: The relation that specifies the "all" condition.

    Example Query in Relational Algebra

    Consider the following two relations:

    1. Students (StudentID, Name)
    2. Enrollments (StudentID, CourseID)

    Query: Find the names of all students who are enrolled in the course with CourseID = 101.

    Using relational algebra, this can be expressed as:

    π(Name)(σ(CourseID = 101)(Enrollments ⨝ Students))
    

    Explanation:

    • Enrollments ⨝ Students: Join the Enrollments table with the Students table on StudentID.
    • σ(CourseID = 101): Apply the selection operation to get only the tuples where CourseID is 101.
    • π(Name): Finally, apply the projection operation to return only the Name attribute of the students.

    Conclusion

    Relational algebra is a fundamental part of relational database theory, providing the theoretical foundation for query languages like SQL. By combining basic operations like selection, projection, union, and join, relational algebra allows us to express complex queries and operations on relational databases in a formal, systematic way. Understanding these operations is crucial for optimizing queries and understanding how relational databases process and manipulate data.

    Previous topic 13
    Integrity Constraints
    Next topic 15
    Selection in Relational Algebra

    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 time7 min
      Word count1,124
      Code examples0
      DifficultyIntermediate