Lecture 10-Relational Algebra
Lecture 10-Relational Algebra
Lecture 10-Relational Algebra
Next slide…
The following query results refer to this database state
Unary Relational Operations: SELECT
• SELECT operation (denoted by s (sigma)) is used to select a subset of the
tuples from a relation R based on a selection condition.
s <selection condition>(R)
• The <selection condition> acts as a filter:
Tuples satisfying the condition are selected whereas the other tuples are
discarded (filtered out)
• Examples:
– Select the EMPLOYEE tuples whose department number is 4:
s DNO = 4 (EMPLOYEE)
– Select the employee tuples whose salary is greater than $30,000:
s SALARY > 30,000 (EMPLOYEE)
Slide 8- 10
Relational Algebra Expressions
• We may want to apply several relational algebra
operations one after the other
– Either we can write the operations as a single relational
algebra expression by nesting the operations, or
– We can apply one operation at a time and create
intermediate result relations.
• In the latter case, we must give names to the
relations that hold the intermediate results.
Slide 8- 24
JOIN Variations
• THETA-JOIN
– The general case of JOIN operation
R theta S
– The join condition is called theta
– Theta can be any general Boolean expression on the attributes of R and S; e.g.,
R.Ai <= S.Bj AND ( R.Ak = S.Bi OR R.Ap <> S.Bq )
• EQUIJOIN
– The JOINT operation where the only comparison operator used is =
R R.Aj = S.Bk S
– equality comparisons are commonly used to combine related tuples from
various relations.
– The JOIN seen in Example 8.6 is an EQUIJOIN:
DEPARTMENT Mgr_ssn=Ssn EMPLOYEE
Note that the names of the two join attributes need not be identical
Slide 8- 38
Additional Relational Operations (continued)
• OUTER UNION Operations
– 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 R(X, Y) and
S(X, Z) that are partially compatible, meaning that only some of their
attributes, say X, are type compatible.
– The attributes that are type compatible are represented only once in
the result, and those attributes that are not type compatible from
either relation are also kept in the result relation T(X, Y, Z).
n Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
RESEARCH_DEPT ¬ s DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS ¬ (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE)
RESULT ¬ p FNAME, LNAME, ADDRESS (RESEARCH_EMPS)
Slide 8- 41
Examples of Queries in Relational Algebra –
Single expressions
Slide 8- 42