Relational Algebra
Relational algebra and relational calculus are formal languages associated with the
relational model.
Informally, relational algebra is a (high-level) procedural language and relational
calculus a non-procedural language.
Closure
Closure is the property that ensures the result of any relational algebra operation is
itself a relation, which can be further used as input for other relational algebra
operations.
Five basic operations in relational algebra: Selection, Projection, Cartesian product,
Union & Set Difference.
Also have Join, Intersection & Division operations, which can be expressed in terms of
5 basic operations.
Selection
condition (R) : Works on a single relation R and defines a relation that contains only
those rows of R that satisfy the specified condition.
Example: List all staff with a salary greater than $10,000.
salary > 10000 (Staff)
Projection
col1, . . . , coln(R): Operates on a single relation R and returns a new relation with only
the specified columns (attributes) from R, removing any duplicate rows.
Example: Produce a list of salaries for all staff, showing only staffNo, fName, lName,
and salary details.
staffNo, fName, lName, salary(Staff)
Union
R S: The union of two relations R and S creates a new relation that includes all unique
rows from R, S, or both, with duplicates removed.
Example: List all cities where there is either a branch office or a property for rent.
city(Branch) city(PropertyForRent)
Set Difference
R – S: Creates a relation with rows that are in R but not in S. R and S must have the same
structure (union-compatible).
Example: List all cities where there is a branch office but no properties for rent.
city(Branch) – city(PropertyForRent)
Intersection
R S : Creates a relation with all rows that are common to both R and S. R and S must
have the same structure (union-compatible).
Example: List all cities where there is both a branch office and at least one property for
rent.
city(Branch) city(PropertyForRent)
Cartesian product
R X S: Creates a relation by combining each row of R with every row of S.
Example: List the names and comments of all clients who have viewed a property for
rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment (Viewing))
Join Operations
Join is based on the Cartesian product. It works by first combining all rows from two
relations, then filtering them using a condition (join condition).
Example: Use selection operation to extract those tuples where Client.clientNo =
Viewing.clientNo.
sClient.clientNo = Viewing.clientNo ( ( clientNo, fName, lName(Client) ) ( clientNo, propertyNo,
comment(Viewing) ) )
Various forms of join operation :-
• Theta join
• Equijoin (a particular type of Theta join)
• Natural join
• Outer join
• Semijoin