Tut8 (Ans)

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

SEMESTER 1 05/06 WXES2103 DATABASE

SQL
TUTORIAL 8 (Answer Scheme)
1.

Consider the insurance database of Figure 1 below, where the primary keys are
underlined. Construct the following SQL queries for this relational database.
person (driver-id#, name, address)
car (license, model, year)
accident (report-number, date, location)
owns (driver-id#, license)
participated (driver-id#, car, report-number, damage-amount)
Figure 1 Insurance database
a.
b.
c.
d.
e.

Find the total number of people who owned cars that were involved in
accidents in 1989.
Find the number of accidents in which the cars belonging to John Smith
were involved.
Add a new accident to the database; assume any values for required
attributes.
Delete the Mazda belonging to John Smith.
Update the damage amount for the car with license number AABB2000
in the accident with report number AR2197 to $3000.

Answers: Note: The participated relation relates drivers, cars, and accidents.
a.

Note: this is not the same as the total number of accidents in 1989. We must
count people with several accidents only once.
select count (distinct name)
from accident, participated, person
where accident.report-number = participated.report-number
and participated.driver-id = person.driver-id
and date between date 1989-00-00 and date 1989-12-31

b.
select count (distinct *)
from accident
where exists
(select *
from participated, person
where participated.driver-id = person.driver-id
and person.name = John Smith
and accident.report-number = participated.report-number)

c. We assume the driver was Jones, although it could be someone else. Also, we
assume Jones owns one Toyota. First we must find the license of the given car.
Then the participated and accident relations must be updated in order to both
record the accident and tie it to the given car. We assume values Berkeley for
location, 2001-09-01 for date and date, 4007 for reportnumber and 3000 for
damage amount.
insert into accident
values (4007, 2001-09-01, Berkeley)
insert into participated
select o.driver-id, c.license, 4007, 3000
from person p, owns o, car c
where p.name = Jones and p.driver-id = o.driver-id and
o.license = c.license and c.model = Toyota
d.

Since model is not a key of the car relation, we can either assume that only one
of John Smiths cars is a Mazda, or delete all of John Smiths Mazdas (the query
is the same). Again assume name is a key for person.
delete car
where model = Mazda and license in
(select license
from person p, owns o
where p.name = John Smith and p.driver-id = o.driver-id)
Note: The owns, accident and participated records associated with the Mazda
still exist.

e.
update participated
set damage-amount = 3000
where report-number = AR2197 and driver-id in
(select driver-id
from owns
where license = AABB2000)

2.

Consider the employee database of Figure 2 below, where the primary keys are
underlined. ive an expression in SQL for each of the following queries.
a.
b.
c.
d.
e.

Find the names of all employees who work for First Bank Corporation.
Find the names and cities of residence of all employees who work for
First Bank Corporation.
Find the names, street addresses, and cities of residence of all
employees who work for First Bank Corporation and earn more than
$10,000.
Find all employees in the database who live in the same cities as the
companies for which they work.
Find the company that has the smallest payroll.

employee (employee-name, street, city)


works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Figure 2. Employee database
Answers:
a.
select employee-name
from works
where company-name = First Bank Corporation
b.
select e.employee-name, city
from employee e, works w
where w.company-name = First Bank Corporation and w.employee-name =
e.employee-name
c.
If people may work for several companies, the following solution will
only list those who earn more than $10,000 per annum from First Bank
Corporation alone.
select *
from employee
where employee-name in
(select employee-name
from works
where company-name = First Bank Corporation and salary > 10000)
As in the solution to the previous query, we can use a join to solve this one also.

d.
select e.employee-name
from employee e, works w, company c
where e.employee-name = w.employee-name and e.city = c.city and
w.company -name = c.company name
e.
select company-name
from works
group by company-name
having sum (salary) <= all (select sum (salary)
from works
group by company-name)

You might also like