01 ct01
01 ct01
1. Construct an E-R diagram for a car insurance company whose customers own one or more cars
each. Each car has associated with it zero to any number of recorded accidents. Each insurance
policy covers one or more cars and has one or more premium payments associated with it. Each
payment is for a particular period of time, and has an associated due date, and the date when the
payment was received. (8 Marks)
Answer
2. Consider the employee database shown below. Give an expression in the relational algebra to
express each of the following queries: (4 Marks)
Employee Database
employee (id, name, street, city)
works (id, company name, salary)
company (company name, city)
(i) Find the ID and name of each employee who works for “BigBank”.
Answer
(ii) Find the ID and name of each employee in this database who lives in the same
city as the company for which she or he works.
Answer
3. Consider the library database shown below. Write the following queries in SQL. (8 Marks)
Library Database
member(memb_no, name)
book(isbn, title, authors, publisher)
borrowed(memb_no, isbn, date)
(i) Find the member number and name of each member who has borrowed at least one book
published by “McGraw-Hill”.
Answer
SELECT
memb_no, name
FROM
member AS m
WHERE EXISTS (
SELECT *
FROM
book
INNER JOIN
borrowed
ON
book.isbn = borrowed.isbn
WHERE
book.publisher = 'McGraw-Hill' AND borrowed.memb_no = m.memb_no
)
(ii) For each publisher, find the member number and name of each member who has borrowed more
than five books.
Answer
WITH
member_borrowed_book(memb_no, memb_name,isbn,title,authors,publisher,date)
AS (
SELECT
member.memb_no, name, book.isbn, title, authors, publisher, date
FROM
member INNER JOIN borrowed ON member.memb_no = borrowed.memb_no
INNER JOIN
book
ON
borrowed.isbn = book.isbn
)
SELECT
memb_no, memb_name, publisher, COUNT(isbn)
FROM
member_borrowed_book
GROUP BY
memb_no, memb_name, publisher
HAVING COUNT(isbn) > 5;