0% found this document useful (0 votes)
19 views24 pages

W2-EX RA0 6 Solutions

The document contains exercises and solutions related to relational algebra queries on a COMPANY and LIBRARY database schema. It includes specific queries to retrieve employee information, project details, and library data, along with the relational algebra expressions for each query. The exercises aim to demonstrate the application of relational operators to extract meaningful data from the given database states.

Uploaded by

LR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views24 pages

W2-EX RA0 6 Solutions

The document contains exercises and solutions related to relational algebra queries on a COMPANY and LIBRARY database schema. It includes specific queries to retrieve employee information, project details, and library data, along with the relational algebra expressions for each query. The exercises aim to demonstrate the application of relational operators to extract meaningful data from the given database states.

Uploaded by

LR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Exercise 6.

15 RA0

Show the result of each of the sample queries in


Section 6.5 (Example Queries 1-7) as it would apply
to the database state in Figure 3.6.

1
Exercise 6.15 Solutions

2
Exercise 6.15 Solutions

3
Exercise 6.16 RA0
Specify the following queries on the COMPANY relational database schema shown in
Figure 3.5, using the relational operators discussed in this chapter. Also show the
result of each query as it would apply to the database state in Figure 3.6.


a. Retrieve the names of all employees in department 5 who work more than 10 hours
per week on the ProductX project.

b. List the names of all employees who have a dependent with the same first name as
themselves.

c. Find the names of all employees who are directly supervised by ‘Franklin Wong’.

d. For each project, list the project name and the total hours per week (by all
employees) spent on that project.

e. Retrieve the names of all employees who work on every project.

f. Retrieve the names of all employees who do not work on any project.

g. For each department, retrieve the department name and the average salary of all
employees working in that department.

h. Retrieve the average salary of all female employees.

i. Find the names and addresses of all employees who work on at least one project
located in Houston but whose department has no location in Houston.

j. List the last names of all department managers who have no dependents. 4
Exercise 6.16 Solutions
a. Retrieve the names of all employees in department 5 who
work more than 10 hours per week on the ProductX project.
EMPWORKX ←(σ Pname= ' ProductX ' (PROJECT ))∞ Pnumber= Pno (WORKS _ ON )

EMPWORK 10←(EMPLOYEE)∞ Ssn= Essn ( σ Hours >10 (EMPWORKX ))

RESULT ← π Lname , Fname ( σ Dno=5 (EMPWORK 10))

5
Exercise 6.16 Solutions

b. List the names of all employees who have a
dependent with the same first name as themselves.
E←(EMPLOYEE)∞( Ssn= Essn, Fname= Dependent _ Name) (DEPENDENT )
R← π Lname , Fname ( E)
Join with two attributes

6
Exercise 6.16 Solutions

c. Find the names of all employees who are directly
supervised by ‘Franklin Wong’.
WONG _ SSN ←π Ssn (σ Fname= ' Franklin'∧ Lname=' Wong' ( EMPLOYEE))

WONG _ EMPS←(EMPLOYEE)∞( Super _ ssn= Ssn)(WONG _ SSN )

RESULT ← π Lname , Fname (WONG _ EMPS)

7
Exercise 6.16 Solutions

d. For each project, list the project name and the total
hours per week (by all employees) spent on that project.
PROJ _ HOURS( Pno , Tot _ hrs) ← Pno ℑSUM Hours (WORKS _ ON )

RESULT ← π Pname , Tot _ hrs (( PROJ _ HOURS )∞( Pno= Pnumber ) ( PROJECT ))

8
Exercise 6.16 Solutions

e. Retrieve the names of all employees who work
on every project.
PROJ _ EMPS( Pno, Ssn) ←π Pno , Essn (WORKS _ ON )

ALL _ PROJS ( Pno)←π Pnumber (PROJECT )

EMPS _ ALL _ PROJS ← PROJ _ EMPS÷ ALL _ PROJS

RESULT ← π Lname , Fname ( EMPLOYEE∗EMPS _ ALL _ PROJS)

9
Exercise 6.16 Solutions

f. Retrieve the names of all employees who do
not work on any project.

ALL _ EMPS ←π Ssn ( EMPLOYEE)

WORKING _ EMPS (Ssn )← π Essn (WORKS _ ON )

NON _ WORKING _ EMPS ← ALL _ EMPS−WORKING _ EMPS

RESULT ← π Lname , Fname ( EMPLOYEE∗NON _ WORKING _ EMPS)

10
Exercise 6.16 Solutions

g. For each department, retrieve the department name and the
average salary of all employees working in that department.

DEPT _ AVG _ SALS Dnumber , Avg _ sal ← Dno ℑ AVG Salary ( EMPLOYEE)

RESULT ← π Dname , Avg _ sal ( DEPT _ AVG _ SALS∗DEPARTMENT )

11
Exercise 6.16 Solutions

h. Retrieve the average salary of all female
employees.
RESULT AVG _ F _ SAL ← ℑAVG Salary (σ Sex=' F ' EMPLOYEE)
We could group by sex

12
Exercise 6.16 Solutions

i. Find the names and addresses of all employees who work
on at least one project located in Houston but whose
department has no location in Houston.

E _ P _ HOU (Ssn) ←π Essn (WORKS _ ON ∞(Pno),( Pnumber) (σ Plocation=' Houston ' ( PROJECT )))

D _ NO _ HOU (Ssn) ← π Dnumber (DEPARTMENT )−πDnumber ( σ Dlocation=' Houston' ( DEPT _ LOCATIONS))

E _ D _ NO _ HOU ← πSsn ((EMPLOYEE) ∞(Pno),( Dnumber) (D _ NO _ HOU ))

RESULT _ EMPS ← E _ P _ HOU ∩ E _ D _ NO _ HOU

RESULT ← π Lname , Fname , Address ( EMPLOYEE∗RESULT _ EMPS )

13
Exercise 6.16 Solutions

j. List the last names of all department
managers who have no dependents.
DEPT _ MANAGERS (Ssn) ←π MgrEssn ( DEPARTMENT)

EMPS _ WITH _ DEPENDENTS (Ssn) ← π Essn (DEPENDENT )

RESULT _ EMPS ← DEPT _ MANAGERS−EMPS _ WITH _ DEPENDENTS

RESULT ← π Lname , Fname ( EMPLOYEE∗RESULT _ EMPS)

14
Exercise 6.18
Consider the LIBRARY relational database 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:

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.

d. For each book that is loaned out from the Sharpstown branch and whose
Due_date 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 coauthored) by Stephen King, retrieve the title and
the number of copies owned by the library branch whose name is Central. 15
Exercise 6.18 Solutions

a. How many copies of the book titled “The Lost Tribe” are
owned by the library branch whose name is ‘Sharpstown’?
THE _ BOOK ←σTitle=' The Lost Tribe ' (BOOK )

RESULT ← π Noofcopies (σ BranchName=' Sharpstown' (LIBRARY _ BRANCH )∗(BOOKCOPIES∗THE _ BOOK))

From BOOKCOPIES
Using Book_id
Using Branch_id

16
Exercise 6.18 Solutions

b. How many copies of the book titled The Lost Tribe are
owned by each library branch?

π BranchID , NoOfCopies (σ Title =' The Lost Tribe ' ( BOOK )∗BOOKCOPIES )
From BOOKCOPIES
Using Book_id

17
Exercise 6.18 Solutions


c. Retrieve the names of all borrowers who do not have any
books checked out. people with books on
All loan
borrowers

NO _ CHECKOUT _ B← πCardNo (BORROWER)−π CardNo(BOOK _ LOANS )

RESULT ← π Pname (BORROWER∗NO _ CHECKOUT _ B)

18
Exercise 6.18 Solutions

d. For each book that is loaned out from the Sharpstown branch
and whose Due_date is today, retrieve the book title, the
borrower’s name, and the borrower’s address.
Using
BranchId
SHARP ←π BranchId ( σ BranchName=' Sharpstown ' (LIBRARY _ BRANCH ))

TODAY _ FR _ S ← πBookId ,CardNo ((σ DueDate =' Today' (BOOKLOANS )∗SHARP))

RESULT ← πTitle , Name, Address ( BOOK∗BORROWER∗TODAY _ FR _ S)


Using Book_id Using CardNo

19
Exercise 6.18 Solutions

e. For each library branch, retrieve the branch name and
the total number of books loaned out from that branch.
TOTALS(BranchId ,Total) ← BranchId ℑ COUNT (BookId ,CardNo) (BOOK _ LOANS)
We need to count by BOTH BookId AND
CardNumber because a BookId can be
loaned out to several borrowers.

RESULT ← πBranchName ,Total (TOTALS∗LIBRARY _ BRANCH )

20
Exercise 6.18 Solutions

f. Retrieve the names, addresses, and number of books checked
out for all borrowers who have more than five books checked out.

COUNTBORROWS(CardNo ,TotalCheckout) ← CardNoℑCOUNT (BookId ) ( BOOK _ LOANS )


Counts the number of
books for each cardholder
BORROW 5←σ TotalCheckout>5 (COUNTBORROWS )

RESULT ← π Name, Address ,TotalCheckout (BORROW 5∗BORROWER)


CardNo

21
Exercise 6.18 Solutions


g. For each book authored (or coauthored) by Stephen King, retrieve
the title and the number of copies owned by the library branch whose
name is Central.

STEPHENK (BookId , Title) ←(σ AuthorName =' Stephen King ' (BOOK _ AUTHORS))∗BOOK

CENTRAL(BranchId ) ←σ BranchName=' Central ' (LIBRARY _ BRANCH )

RESULT ← πTitle , NoOfCopies (STEPHENK∗BOOKCOPIES∗CENTRAL)

We need natural joins in order to


get the attributed required

22
Exercise 6.22
Consider the two tables T1 and T2 shown in Figure 6.15.
Show the results of the following operations:

23
Exercise 6.22 Solutions

24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy