Chapter 8: The Relational Algebra and Relational Calculus
1
CHAPTER 8: THE RELATIONAL ALGEBRA AND RELATIONAL CALCULUS
Answers to Selected Exercises
8.15 – Show the result of each of the sample queries in Section 6.5 as it would apply to the
database state in Figure 3.6.
Answer:
(QUERY 1) Find the name and address of all employees who work for the ‘Research’
department.
(QUERY 2) For every project located in ‘Stafford’, list the project number, the controlling
department number, and the department manager’s last name, address, and birth date.
Result:
(QUERY 3) Find the names of all employees who work on all the projects controlled by
department number 5.
Result: (empty because no tuples satisfy the result).
(QUERY 4) Make a list of project numbers for projects that involve an employee whose last
name is ‘Smith’ as a worker or as a manager of the department that controls the project.
Result: PNO
1
2
(QUERY 5) List the names of all employees with two or more dependents.
Result: LNAME FNAME
Smith John
Wong Franklin
(QUERY 6) List the names of employees who have no dependents.
Result: LNAME FNAME
Zelaya Alicia
Narayan Ramesh
English Joyce
Wallace Jennifer
Wong Franklin
Chapter 8: The Relational Algebra and Relational Calculus
3
Result (empty):
LNAME FNAME
(c) WONG_SSN < P SSN ( s FNAME=’Franklin’ AND
LNAME=’Wong’ (EMPLOYEE))
(d) PROJ_HOURS(PNO,TOT_HRS) < PNO f SUM HOURS (WORKS_ON)
RESULT < P PNAME,TOT_HRS ( (PROJ_HOURS) J (PNO),(PNUMBER)
(PROJECT) )
Result:
PNAME TOT_HRS
(e) PROJ_EMPS(PNO,SSN) < P PNO,ESSN (WORKS_ON)
ALL_PROJS(PNO) < P PNUMBER (PROJECT)
(f) ALL_EMPS < P SSN (EMPLOYEE)
WORKING_EMPS(SSN) < P ESSN (WORKS_ON)
NON_WORKING_EMPS < ALL_EMPS – WORKING_EMPS (* DIFFERENCE
*)
(g) DEPT_AVG_SALS(DNUMBER,AVG_SAL) < DNO f AVG SALARY
(EMPLOYEE)
Chapter 8: The Relational Algebra and Relational Calculus
4
(h) RESULT(AVG_F_SAL) < f AVG SALARY ( s SEX=’F’ (EMPLOYEE) )
Result:
(i) E_P_HOU(SSN) <
P ESSN (WORKS_ON J(PNO),(PNUMBER) ( s PLOCATION=’Houston’
(PROJECT)))
D_NO_HOU <
Result:
LNAME FNAME ADDRESS
Wallace Jennifer 291 Berry, Bellaire, TX
LNAME FNAME
Borg James
8.17 No solution provided.
8.18 – Consider the LIBRARY relational schema shown in Figure 6.14, which is used to keep
track of books, borrowers, and book loans. Referential integrity constraints are shown as
directed arcs in Figure 6.14, as in the notation of Figure 3.7. Write down relational
expressions for the following queries on the LIBRARY database:
(a) How many copies of the book titled The Lost Tribe are owned by the library branch
whose name is “Sharpstown”?
(b) How many copies of the book titled The Lost Tribe are owned by each library branch?
(c) Retrieve the names of all borrowers who do not have any books checked out.
Chapter 8: The Relational Algebra and Relational Calculus
5
(d) For each book that is loaned out from the “Sharpstown” branch and whose DueDate
is today, retrieve the book title, the borrower’s name, and the borrower’s address.
(e) For each library branch, retrieve the branch name and the total number of books
loaned out from that branch.
(f) Retrieve the names, addresses, and number of books checked out for all borrowers
who have more than five books checked out.
(g) For each book authored (or co-authored) by “Stephen King”, retrieve the title and the
number of copies owned by the library branch whose name is “Central”.
Answer:
(Note: We will use S for SELECT, P for PROJECT, * for NATURAL JOIN, for
SET DIFFERENCE, F for AGGREGATE FUNCTION)
(a) A < BOOKCOPIES * LIBRARY-BRANCH * BOOK
RESULT < P No_Of_Copies ( S BranchName=‘Sharpstown’ and Title=’The Lost
(b) P BranchID,No_Of_Copies ( ( S Title=’The Lost Tribe’ (BOOK)) * BOOKCOPIES )
(c) NO_CHECKOUT_B < P CardNo (BORROWER) – P CardNo (BOOK_LOANS)
(d) S < P BranchId ( S BranchName=‘Sharpstown’ (LIBRARY-BRANCH) )
(e) R(BranchId,Total) < BranchId FCOUNT(BookId,CardNo) (BOOK_LOANS)
(f) B(CardNo,TotalCheckout) < CardNo F COUNT(BookId) (BOOK_LOANS)
(g) SK(BookId,Title) < ( sAuthorName=’Stephen King’ ( BOOK_AUTHORS)) * BOOK
8.19 8.21: No solutions provided.
8.22 Consider the two tables T1 and T2 shown in Figure 6.15. Show the results of the
following operations:
Chapter 8: The Relational Algebra and Relational Calculus
6
Answers:
(a)
P Q R A B C
(b)
15 b 8 10 b 5
(c)
P Q R A B C
10 a 5 10 b 6
(d)
P Q R A B C
(e)
P Q R
10a 5
(f)
8.23 No solution provided.
8.24 – Specify queries (a), (b), (c), (e), (f), (i), and (j) of Exercise 6.16 in both tuple and
domain relational calculus.
Answer:
(a) Retrieve the names of employees in department 5 who work more than 10 hours per
week on the ‘ProductX’ project.
Chapter 8: The Relational Algebra and Relational Calculus
7
{ qs | EMPLOYEE(qrstuvwxyz) AND z=5 AND (EXISTS a) (EXISTS b) (EXISTS
(b) List the names of employees who have a dependent with the same first name as
themselves.
(c) Find the names of employees that are directly supervised by ‘Franklin Wong’.
Tuple relational Calculus:
{ e.LNAME, e.FNAME | EMPLOYEE(e) AND (EXISTS s) ( EMPLOYEE(s) AND
(e) Retrieve the names of employees who work on every project.
Tuple relational Calculus:
{ e.LNAME, e.FNAME | EMPLOYEE(e) AND (FORALL p) ( NOT(PROJECT(p))
OR
(EXISTS w) (
(f) Retrieve the names of employees who do not work on any project.
Tuple relational Calculus:
{ e.LNAME, e.FNAME | EMPLOYEE(e) AND NOT(EXISTS w) ( WORKS_ON(w)
AND
(i) Find the names and addresses of employees who work on at least one project located
Chapter 8: The Relational Algebra and Relational Calculus
8
in Houston but whose department has no location in Houston.
Tuple relational Calculus:
{ e.LNAME, e.FNAME, e.ADDRESS | EMPLOYEE(e) AND (EXISTS p) (EXISTS
w) (
c=’Houston’ AND
NOT(EXISTS h) NOT(EXISTS i) ( DEPT_LOCATIONS(hi) AND z=h AND
i=’Houston’
) ) }
(j) List the last names of department managers who have no dependents.
Tuple relational Calculus:
{ e.LNAME | EMPLOYEE(e) AND (EXISTS d) ( DEPARTMENT(d) AND
e.SSN=d.MGRSSN AND
NOT(EXISTS x) (DEPENDENT(x) AND e.SSN=x.ESSN) ) }
AND NOT(EXISTS e) (DEPENDENT(efghi) AND e=t) ) }
8.25 – No solution provided.
6.26 Specify queries c, d, and e of Exercise 6.18 in both tuple and domain relational
calculus.
Answer:
(c) For each section taught by professor King, retrieve the course number, semester,
(d) Retrieve the name and transcript of each senior student (Class=5) majoring in
COSC. Transcript includes course name, course number, credit hours, semester, year, and
grade for each course completed by the student.
Chapter 8: The Relational Algebra and Relational Calculus
9
s.Major=’COSC’ AND s.StudentNumber=g.StudentNumber AND
g.SectionIdentifier=t.SectionIdentifier
AND t.CourseNumber=c.CourseNumber}
Domain relational Calculus:
(e) Retrieve the names and major departments of all straight A students (students
who have a grade of A in all their courses).
Tuple relational Calculus:
{ s.Name, s.Major | STUDENT(s) AND (FORALL g) ( NOT(GRADE_REPORT(g))
OR
8.27 – In a tuple relational calculus query with n tuple variables, what would be the typical
minimum number of join conditions? Why? What is the effect of having a smaller number of
join conditions?
Answer:
8.28 – Rewrite the domain relational calculus queries that followed Q0 in Section 6.7 in the
style of the abbreviated notation of Q0A, where the objective is to minimize the number of
domain variables by writing constants in place of variables wherever possible.
Answer:
Q1A: { qsv | (EXISTS z) (EXISTS m) ( EMPLOYEE(q,r,s,t,u,v,w,x,y,z) AND
8.29 No solution provided.
8.30 – Show how you can specify the following relational algebra operations in both tuple and
domain relational calculus.
Chapter 8: The Relational Algebra and Relational Calculus
10
(a) SELECT A=c (R(A, B, C)):
(b) PROJECT <A, B> (R(A, B, C)):
(c) R(A, B, C) NATURAL JOIN S(C, D, E):
(d) R(A, B, C) UNION S(A, B, C):
(e) R(A, B, C) INTERSECT S(A, B, C):
(f) R(A, B, C) MINUS S(A, B, C):
(g) R(A, B, C) CARTESIAN PRODUCT S(D, E, F):
(h) R(A, B) DIVIDE S(A):
Answer:
For each operation, we give the tuple calculus expression followed by the domain calculus
expression.
(a) { t | R(t) AND t.A=c}, { xyz | R(xyz) AND x=c }
(b) { t.A, t.B | R(t) }, { xy | R(xyz) }
(f) { t | R(t) AND NOT(S(t)) },
{ xyz | R(xyz) AND NOT(S(xyz)) }
(g) { t.A, t.B, t.C, q.D, q.E, q.F | R(t) AND S(q) },
( xyzuvw | R(xyz) AND S(uvw) }
8.31 No solution provided.
8.32 – A nested query is query within a query. More specifically, a nested query is a
parenthesized query that can be used as a value in a number of places, such as instead of a
relation or a selection condition. Specify the following queries on the database specified in
Figure 3.5 using the concept of nested queries and the relational operators discussed in this
chapter. Also show the result of each query as it would apply to the database state of Figure