0% found this document useful (0 votes)
35 views6 pages

Homework 2 Solutions

Uploaded by

Krystal Yeung
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)
35 views6 pages

Homework 2 Solutions

Uploaded by

Krystal Yeung
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/ 6

CS3402 Database Systems

Homework 2

1) (SQL Queries, 50 marks) Consider the following ER diagram that models a


hotel chain that owns multiple hotels.

Assume that there already exist SQL tables for the corresponding relational
schema:
Hotel( hname: string, city: string)
Room( hname: string, rnumber: int, size: int );
Guest( passport: string, name: string, date_of_birth: date )
Booking( hname: string, rnumber: string, passport: string, date_of_stay: date )

Write SQL queries for the following tasks:


a. List all attributes for hotels located either in Toronto or in Tokyo. [5 marks]
b. Compute the total number of rooms of all hotels in Zurich. [6 marks]
c. For all guests that have the same birthdate, compute the average length of
their name. [6 marks]
d. List the names of all hotels that have at least 50 rooms. [6 marks]
e. List the passport numbers of all guests that have stayed in a room with
number 1 of any hotel located in Paris. [6 marks]
f. Define the total space of a hotel to be the sum of the sizes of all its rooms.
List the names of all hotels in Rome that each have a total space of at
least 10,000. [7 marks]
g. List the city of every hotel that has a room such that its size ≥50 and the
room was booked by at least 10 guests. (Output each city name at most
once.) [7 marks]
h. List the name of every hotel in Madrid that has more rooms than each one
of the hotels in Oslo. [7 marks]
2) (Relational Algebra, 50 marks) Specify the following queries on the
COMPANY relational database schema shown in Figure 5.5 below using
relational algebra expressions.

a. List the names of all dependents of employees working in departments


located in Houston. [7 marks]
b. List the names of all departments whose manager earns at least 10000.
[7 marks]
c. List the first names of all female employees that work on some project that is
controlled by the Research department. [7 marks]
d. List the first names of all female employees that work on all projects that are
controlled by the Research department. [8 marks]
e. List the SSN of employees who do not have any dependents. [7 marks]
f. List the salary of each employee who is supervising at least one other
employee. [7 marks]
g. List the first names and addresses of the employees that work in a
department that has its location in either Sugarland or Bellaire and who work
on at least one project located in Houston (i.e. Plocation=’Houston’).
[7 marks]
Solutions
Question 1
a)
select *
from hotel
where city='Toronto' or city='Tokyo';

b)
select count(*)
from room r, hotel h
where r.hname = h.hname
and h.city = 'Zurich';

c)
select avg(name),date_of_birth
from guest
group by date_of_birth;

d)
select hname
from room r
group by hname
having count(*)>=50;

e)
select passport
from guest g, booking b
where g.passport = b.passport
and rnumber in
(select rnumber from room r, hotel h
where r.hname=h.hname
and r.rnumber = 1
and h.city='Paris');

f)
select hname
from room r,hotel h
where r.hname = h.hname
and h.city = 'Rome'
group by hname
having sum(size)>=10000;

g)
select distinct city
from hotel h1,
(select hname,rnumber
from room r,booking b
where r.rnumber = b.rnumber
and r.hname = b.hname
and r.size >= 50
group by hname,rnumber
having count(*)>=10) h2
where h1.hname = h2.hname;

h)
select hname
from hotel h1
where city='Madrid'
and
(select count(*)
from room r
where r.hname = h1.hname)
> ALL
(select count(*)
from room r, hotel h2
where r.hname = h2.hname
and h2.city = 'Oslo')

Question 2
a)

EMP1 ß s Dlocation=’Houston’ (EMPLOYEE ⨝Dno = Dnumber (DEPARTMENT *


DEPT_LOCATIONS))

p dependent_name (EMP1 ⨝Essn = Ssn DEPENDENT )


b)

p dname(s Salary≥1000 (EMPLOYEE ⨝Ssn = Mgr_ssn DEPARTMENT))


c)

FEM_EMP ß (s Sex=’female’(EMPLOYEE ⨝SSn = Essn WORKS_ON)

PROJ_RES ß p Pnumber(s Dname=’Research’ (DEPARTMENT ⨝Dnumber = Dnum PROJECT))

p fname (PROJ_RES ⨝Pno = Pnumber FEM_EMP)


d)

ALL_PROJ_RES ß s dname=’Research’ (PROJECT ⨝Dnum = Dnumber DEPARTMENT)

ALL_PROJ_RES_PNOS(Pno) ß p Pnumber (ALL_PROJ_RES)

FEM_EMP ß p fname,Pno (s Sex=’female’(EMPLOYEE ⨝SSn = Essn WORKS_ON)

FEM_EMP ÷ ALL_PROJ_RES_PNOS

e)

ALL_EMP ß p Ssn ( EMPLOYEE)


DEP_EMP ß p Ssn ( EMPLOYEE ⨝Essn = Ssn DEPENDENT)
ALL_EMP – DEP_EMP
f)

SUPERVISORS(Ssn) ß p Super_ssn ( EMPLOYEE)


p Salary ( EMPLOYEE * SUPERVISORS )
g)
The following solution considers departments that have a location in Sugarland or
Bellaire or both. Solutions that only consider departments that have a location *either*
in Sugarland or Bellaire are also correct.

Houston_projects ß s PLocation=’Houston’ (PROJECTS ⨝PNumber=PNo WORKS_ON)

Dptmts ß sDLocation=’Sugarland’ or DLocation=’Bellaire’ (DEPARTMENT * DEPT_LOCATION)


Employee_Dptmts ß Dptmts ⨝DNo=DNumber EMPLOYEE

p Fname,Address (Houston_projects ⨝Essn =Ssn Employee_Dptmts)

You might also like