Relational Algebra Problems
Relational Algebra Problems
Ssn=Mgr_ssnDEPARTMENT)) Dnumber=DnumPROJECT)
Query 5. List the names of all employees with two or more
dependents.
Strictly speaking, this query cannot be done in
the basic (original) relational algebra. We have to use
the AGGREGATE FUNCTION operation with the COUNT aggregate
function. We assume that dependents of the same employee
have distinct Dependent_name values.
T1(Ssn, No_of_dependents)← Essn ℑ COUNT Dependent_name (DEPENDENT)
T2 ← σ
No_of_dependents>2(T1)
RESULT ← π (T2 EMPLOYEE)
Lname, Fname *
Query 6. Retrieve the names of employees who have no
dependents.
This is an example of the type of query that uses
the MINUS (SET DIFFERENCE) operation.
ALL_EMPS ← πSsn(EMPLOYEE)
EMPS_WITH_DEPS(Ssn) ← πEssn(DEPENDENT)
EMPS_WITHOUT_DEPS ← (ALL_EMPS – EMPS_WITH_DEPS)
RESULT ← πLname, Fname(EMPS_WITHOUT_DEPS * EMPLOYEE)
We first retrieve a relation with all employee Ssns
in ALL_EMPS. Then we create a table with the Ssns of employees
who have at least one dependent in EMPS_WITH_DEPS. Then we
apply the SET DIFFERENCE operation to retrieve employees Ssns
with no dependents in EMPS_WITHOUT_DEPS, and finally join this
with EMPLOYEE to retrieve the desired attributes. As a single
in-line expression, this query becomes:
π ((π (EMPLOYEE) – ρ (π (DEPENDENT))) EMPLOYEE)
Lname, Fname Ssn Ssn Essn *
Query 7. List the names of managers who have at least one
dependent.
MGRS(Ssn) ← πMgr_ssn(DEPARTMENT)
EMPS_WITH_DEPS(Ssn) ← πEssn(DEPENDENT)
MGRS_WITH_DEPS ← (MGRS ∩ EMPS_WITH_DEPS)
RESULT ← πLname, Fname(MGRS_WITH_DEPS * EMPLOYEE)
In this query, we retrieve the Ssns of managers in MGRS, and
the Ssns of employees with at least one dependent
in EMPS_WITH_DEPS, then we apply
the SET INTERSECTION operation to get the Ssns of managers who
have at least one dependent.
As we mentioned earlier, the same query can be specified in
many different ways in relational algebra. In particular, the
operations can often be applied in various orders. In
addition, some operations can be used to replace others; for
example, the INTERSECTION operation in Q7 can be replaced by
a NATURAL JOIN. As an exercise, try to do each of these sample
queries using different operations.12 We showed how to write
queries as single relational algebra expressions for
queries Q1, Q4, and Q6. Try to write the remaining queries as
single expressions. In Chapters 4 and 5 and in Sections 6.6
and 6.7, we show how these queries are written in other
relational languages.
Let us consider a sample database
Car(RegiNo, Make, ModelYear, Color)
Inspection(RegNo->Car, DateInspected, Period, Evaluation),
Problem((RegNo, DateInspected)->Inspection, ProblemCode)
Driver(RegNo->Car, Name, Accidents)
Only cars of year 1996 model are interesting. The model year
of a car is represented as the value of attribute ModelYear in
table Car. Our first intermediate result consists of tuples
representing cars of model year 1996. This is obtained using
selection
C1996 = ModelYear=1996 (Car)
The cars we are interested in should have been inspected for
the period 1999. Thus we need only the rows that cover that
period. We use selection to retrieve them.
In1999 = Period=1999 (Inspection)
First, we should find out cars that have not been inspected
for year 2000. We cannot solve this by using the table
Inspection alone, because it contains data about those
inspections that have been done, not about nonexisting
inspections. Especially selecting rows where period is not
equal to 2000 results to inspections of all other years but
year 2000. This problem may be solved by finding out the
complement 'cars that are inspected for year 2000'. Actually
we need only their registration numbers.
InR2000= RegNo ( Period=2000 (Inspection))
The cars of model year 1995 and older are retrieved using
select on table Car. Actually we need only their regitration
numbers.
C1995R = RegNo ( ModelYear<=1995 (Car))
Borrower. To select all attributes after the cross product of both relation,
Result:
Write a relational algebra query and sql from above the relation Loan and