Sample Final 2
Sample Final 2
Final Examination
• This is not an open book examination. But you are allowed to bring to the exam one sheet of
paper HAND-WRITTEN on both sides (if needed). On this sheet of paper you can write down what
you think you will need for the exam.
• There is no question during the exam. If you are unsure, write down your assumptions
• The total points is 100
1. Propose an entity-relationship (ER) diagram to model this information system. For each of the
entities, feel free to add attributes if needed.
2. Give a step by step mapping of the ER diagram to the relational model. Explain your decisions.
1
an attribute]
Argue, by means of examples or counterexamples about the correctness of these equivalences. What
happens if in the second, max is replaced by min?
• Describe a way to implement this query efficiently, in particular suggest how to organize R and S
(that is, whether they are sorted [clustered] and if so with regard to which attribute(s), which
kind of index structure is used, etc.)
{E.Ename∣∃FA∃FI∃F(FA.Eid=E.Eid∧FA.Flno=FI.Flno∧FA.Day=FI.Day∧FI.Flno=F.Flno∧F.To=
′Chicago′∧FI.Day≥DATE(′2005−01−01′))}
2
SELECT DISTINCT E.Ename
FROM Employee E
JOIN FlightAttendant FA ON E.Eid = FA.Eid
JOIN FlightInstance FI ON FA.Flno = FI.Flno AND FA.Day = FI.Day
JOIN Flights F ON FI.Flno = F.Flno
WHERE F.To = 'Chicago' AND FI.Day >= '2005-01-01';
3. Find the employees who flew only to Chicago.
SELECT E.Ename
FROM Employee E
JOIN FlightAttendant FA ON E.Eid = FA.Eid
JOIN FlightInstance FI ON FA.Flno = FI.Flno AND FA.Day = FI.Day
JOIN Flights F ON FI.Flno = F.Flno
GROUP BY E.Eid, E.Ename
HAVING COUNT(DISTINCT F.To) = 1 AND MAX(F.To) = 'Chicago';
4. List the employees who have been to all the cities
SELECT E.Ename
FROM Employee E
JOIN FlightAttendant FA ON E.Eid = FA.Eid
JOIN FlightInstance FI ON FA.Flno = FI.Flno AND FA.Day = FI.Day
JOIN Flights F ON FI.Flno = F.Flno
GROUP BY E.Eid, E.Ename
HAVING COUNT(DISTINCT F.To) = (SELECT COUNT(DISTINCT To) FROM Flights);
5. List the employee who has travelled the most [in term of number of flights].
SELECT E.Ename
FROM Employee E
JOIN FlightAttendant FA ON E.Eid = FA.Eid
GROUP BY E.Eid, E.Ename
ORDER BY COUNT(*) DESC
LIMIT 1;