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

SQL and Relational Algebra Queries

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)
76 views6 pages

SQL and Relational Algebra Queries

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

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 [Link] = [Link]
and [Link] = '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 [Link] = [Link]
and rnumber in
(select rnumber from room r, hotel h
where [Link]=[Link]
and [Link] = 1
and [Link]='Paris');

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

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

h)
select hname
from hotel h1
where city='Madrid'
and
(select count(*)
from room r
where [Link] = [Link])
> ALL
(select count(*)
from room r, hotel h2
where [Link] = [Link]
and [Link] = '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)

Common questions

Powered by AI

To list all attributes of hotels located in Toronto or Tokyo, you use the SQL query: `SELECT * FROM Hotel WHERE city='Toronto' OR city='Tokyo';` .

The expression is: `ALL_EMP <- π Ssn (EMPLOYEE) DEP_EMP <- π Ssn (EMPLOYEE ⨝ Essn = Ssn DEPENDENT) ALL_EMP − DEP_EMP` .

The relational algebra expression is: `ALL_PROJ_RES <- σ dname='Research' (PROJECT ⨝ Dnum = Dnumber DEPARTMENT) ALL_PROJ_RES_PNOS(Pno) <- π Pnumber (ALL_PROJ_RES) FEM_EMP <- π fname, Pno (σ Sex='female' (EMPLOYEE ⨝ Ssn = Essn WORKS_ON)) FEM_EMP ÷ ALL_PROJ_RES_PNOS` .

The SQL query to list these passport numbers is: `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');` .

Total space of a hotel is defined as the sum of the sizes of all its rooms. The SQL query to list hotel names in Rome with at least 10,000 total space is: `SELECT hname FROM Room r, Hotel h WHERE r.hname = h.hname AND h.city = 'Rome' GROUP BY hname HAVING SUM(size) >= 10000;` .

The SQL query is: `SELECT hname FROM Room r GROUP BY hname HAVING COUNT(*) >= 50;` .

The SQL query is: `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;` .

The SQL statement is: `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')` .

The relational algebra expression is: `EMP1 <- σ Dlocation='Houston' (EMPLOYEE ⨝ Dno = Dnumber (DEPARTMENT * DEPT_LOCATIONS)) π dependent_name (EMP1 ⨝ Essn = Ssn DEPENDENT)` .

The SQL query to compute the total number of rooms in hotels located in Zurich is: `SELECT COUNT(*) FROM Room r, Hotel h WHERE r.hname = h.hname AND h.city = 'Zurich';` .

You might also like