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.
Selection (σ):
σ_condition(R)
R: The relation (table) to apply the operation to.condition: A logical condition to filter the rows.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.
Projection (π):
π_column1, column2, ... (R)
R: The relation from which to select attributes.column1, column2, ...: The attributes to be included in the result.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.
Union (∪):
R1 ∪ R2
R1 and R2: Two relations to be combined.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.
Set Difference (−):
R1 − R2
R1 and R2: The two relations to compare.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.
Intersection (∩):
R1 ∩ R2
R1 and R2: The two relations to be compared.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.
Cartesian Product (×):
R1 × R2
R1 and R2: The two relations whose Cartesian product is to be computed.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.
Rename (ρ):
ρ(new_name1, new_name2, ...)(R)
new_name1, new_name2, ...: The new names for the attributes.R: The relation whose attributes are to be renamed.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.
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:
Join (⨝):
R1 ⨝ R2Example:
If we want to join the Students table with the Courses table on StudentID:
Students ⨝ Courses
Division (÷):
R1 ÷ R2
R1: The relation whose tuples we are interested in.R2: The relation that specifies the "all" condition.Consider the following two relations:
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.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.
Open this section to load past papers