Week 6. Relational Algebra 1
Relational Algebra Principles
Relational Algebra
Relational algebra is the basic set of operations for the relational model. These operations enable a user to specify basic retrieval requests (queries). The result of an operation is a new relation, which may have been formed from one or more input relations. Hence can be further manipulated.
Link to originalRelational Algebra Expression
A sequence of relational algebra operations forms a relational algebra expression.
We may choose to use single expressions or a sequence of operations:
Link to original
Unary Relational Operations
Unary Relational Operations
SELECT Statement ()
SELECT (Relational Algebra)
The SELECT operation, , is used to select a subset of tuples from a relation based on a selection condition. The condition acts as a filter and only keeps those tuples that satisfy the qualifying condition.
Where denotes the SELECT operator. Where denotes the relation.
Example:
- Select the EMPLOYEE tuples whose department number if 4:
- Select the EMPLOYEE tuples whose salary is greater than £30,000:
Properties:
Link to original
- SELECT produces a relation that has the same schema as .
- SELECT is commutative: .
- May be applied in any order.
- We can replace a cascade with a single conjunction.
PROJECT Statement ()
PROJECT (Relational Algebra)
The PROJECT operation, , is used to keep certain columns (attributes) from a relation and discard all others. PROJECT uses vertical partitioning, the list of specified columns is kept in each tuple, other attributes in each tuple are discarded. Duplicate rows are removed since relations are sets.
Where denotes the PROJECT operator. Where denotes the desired list of attributes from . Where denotes the relation.
Example:
- Select the employee’s first name, last name, and salary:
Properties:
Link to original
- The number of tuples in the result of a projection is always less or equal to the number of tuples in .
RENAME Statement ()
Link to originalRENAME (Relational Algebra)
The RENAME operation, , is used to rename attributes of a relation or the relation name or both. It is useful when a query requires multiple operations and is necessary in some cases. There are three possible cases:
- Change relation name to :
- Change column (attribute) names to :
- Change combined attribute and relation names:
Example:
Link to original
- Apply multiple operations and rename (using ):
Relational Algebra Operations
Relational Algebra Operations
Relations are sets so we can apply set operators. However, we want the results to be relations.
The two operands and must be type-compatible (except for CROSS PROD.).
- Same number of attributes.
- Same domain for attributes.
Properties of operations:
- Both UNION (Relational Operation) and INTERSECTION (Relational Operation) are commutative:
- They can also both be treated as -ary operations applicable to any number of relations as both are associative operations:
UNION
UNION (Relational Operation)
The binary operation UNION, denoted by , creates a relation that includes all tuples that includes all tuples from either operand.
The result of is a relation that includes all tuples that are either in , in , or in both. Duplicate tuples are eliminated.
Examples:
Link to original
- Retrieve the SSNs of all employees who work in dept 5. or directly supervise an employee who works in dept 5.
INTERSECTION
INTERSECTION (Relational Operation)
The binary operation INTERSECTION, denoted by , creates a relation that includes all tuples that are in both operands.
The result of is a relation that includes all tuples that are both in and .
Link to originalDIFFERENCE
DIFFERENCE (Relational Operation)
The binary operation SET DIFFERENCE (also called MINUS or EXCEPT), denoted by , creates a relation that includes all the tuples in the first operand that are not in the second operand.
The result of is a relation that includes all tuples that are in but not .
Link to originalCARTESIAN PRODUCT
Link to originalCARTESIAN PRODUCT (Relational Operation)
The binary operation CARTESIAN PRODUCT (or CROSS PRODUCT), denoted by , creates a relation that combines tuples from two operands in a combinatorial fashion.
The result of is a relation with degree + attributes: . The resulting relation state has one tuple for each combination of tuples - one from and one from .
Operands do not have to be type-compatible.
Generally, cross product is not meaningful but it can precede other operations. To keep only combinations where the DEPARTMENT is related to the EMPLOYEE, we can add a SELECT operation:
Link to original
Binary Relational Operations
JOIN Statement
JOIN (Relational Operation)
We can simplify the sequence of CARTESIAN PRODUCT followed by SELECT into a single operation JOIN.
The general form of a join operation on two relations and is:
Where and can be any relations that result from general relational algebra expressions.
Examples:
- Suppose we want to retrieve the name of the manager of each department. The join condition is
MGRSSN=SSNorDEPT.MGRSSN = EMPLOYEE.SSN.Theta JOIN
Theta JOIN (Relational Operation)
The general case of the JOIN operation is called a Theta-join:
The join condition is called theta. Theta can be any general boolean expression on the attributes of and , e.g.:
Link to originalEQUIJOIN
EQUIJOIN (Relational Operation)
The most common use of join involves join conditions with equality comparisons only, in this case, it is called an EQUIJOIN.
In the result of an EQUIJOIN, we have one or more pairs of attributes that have identical values in the tuple.
Link to originalNATURAL JOIN
Link to originalNATURAL JOIN (Relational Operation)
The NATURAL JOIN operation, denoted by , was created to get rid of the second attribute in an EQUIJOIN condition.
To apply aw natural join on the DNUMBER attributes of DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write:
Link to original
DIVISION Statement
DIVISION (Relational Operation)
The DIVISION operation, identified by , is the inverse of Cartesian Product (SQL). An intuitive property of the operator is that you can consider that:
For a tuple to appear in the result of the DIVISION, the values in must appear in in combination with every tuple in .
Link to original
Additional Relational Operations
Aggregate Functions
Aggregate Function (Relational Operation)
A type of request that cannot be expressed in basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.
We use the Aggregate Functional operation for common aggregations:
- : retrieves maximum salary value from the relation
- : retrieves minimum salary value from the relation
- : retrieves the sum of the Salary from the relation
- : computes the count of employees and their average salary
We can also use a variation of the aggregate operation:
- Grouping attribute placed to the left.
- Aggregate functions placed to the right.
Hence, we can use in order to group employees by their department number and compute the count of employees and average salary per department.
Link to original
OUTER JOIN
OUTER JOIN (Relational Operation)
In NATURAL JOIN (Relational Operation) and EQUIJOIN (Relational Operation), tuples without a matching tuple are eliminated from the join result.
Link to original
Tuples with null in the join attributes are also eliminated. We can use outer joins to conditionally keep certain data we may want.
LEFT OUTER JOIN:
Keeps every tuple in the left relation . If no matching tuple is found in , then the attributes of in the join result are filled with null values.RIGHT OUTER JOIN:
Keeps every tuple in the right relation . Attributes of are filled with null values in unlatching tuples.FULL OUTER JOIN:
Keeps every tuple in both relations. When no matching tuples are found, pad with null values.
OUTER UNION
OUTER UNION (Relational Algebra)
The OUTER UNION operation was developed to take the union of tuples from two relations if the relations are not type compatible. This operation will take the union of tuples in two relations and that are partially compatible, meaning only some of their attributes (i.e. ) are type compatible.
Attributes not type compatible are kept in the result relation, e.g. .
Link to original