SQL Text Book Exercise Answers
SQL Text Book Exercise Answers
3
Introdu
tion to SQL
Answer:
a. Find the titles of
ourses in the Comp. S
i. department that have 3
redits.
sele
t title
from
ourse
where dept name = 'Comp. S
i.' and
redits = 3
b. Find the IDs of all students who were taught by an instru
tor named Ein-
stein; make sure there are no dupli
ates in the result.
This query
an be answered in several dierent ways. One way is as fol-
lows.
sele
t distin
t takes.ID
from takes, instru
tor, tea
hes
where takes.
ourse id = tea
hes.
ourse id and
takes.se
id = tea
hes.se
id and
takes.semester = tea
hes.semester and
takes.year = tea
hes.year and
tea
hes.id = instru
tor.id and
instru
tor.name = 'Einstein'
. Find the highest salary of any instru
tor.
sele
t max(salary)
from instru
tor
9
10 Chapter 3 Introdu
tion to SQL
d. Find all instru
tors earning the highest salary (there may be more than
one with the same salary).
Note that if the result of the subquery is empty, the aggregate fun
tion
ountreturns a value of 0.
One way of writing the query might appear to be:
But note that if a se
tion does not have any students taking it, it would
not appear in the result. One way of ensuring su
h a se
tion appears with
a
ount of 0 is to use the outer join operation,
overed in Chapter 4.
f. Find the maximum enrollment, a
ross all se
tions, in Fall 2017.
One way of writing this query is as follows:
Pra
ti
e Exer
ises 11
sele
t max(enrollment)
from (sele
t
ount(ID) as enrollment
from se
tion, takes
where takes.year = se
tion.year
and takes.semester = se
tion.semester
and takes.
ourse id = se
tion.
ourse id
and takes.se
id = se
tion.se
id
and takes.semester = 'Fall'
and takes.year = 2017
group by takes.
ourse id, takes.se
id)
It is also possible to write the query without the with
lause, but the sub-
query to nd enrollment would get repeated twi
e in the query.
While not in
orre
t to add distin
t in the
ount, it is not ne
essary in light
of the primary key
onstraint on takes.
12 Chapter 3 Introdu
tion to SQL
3.2
Answer:
a. Find the total grade-points earned by the student with ID 12345, a
ross
all
ourses taken by the student.
sele
t sum(
redits *points)
fromtakes,
ourse, grade points
where takes.grade = grade points.grade
and takes.
ourse id =
ourse.
ourse id
and ID = 12345
In the above query, a student who has not taken any
ourse would not
have any tuples, whereas we would expe
t to get 0 as the answer. One way
of xing this problem is to use the outer join operation, whi
h we study
later in Chapter 4. Another way to ensure that we get 0 as the answer is
via the following query:
(sele
t sum(
redits * points)
from takes,
ourse, grade points
where takes.grade = grade points.grade
and takes.
ourse id =
ourse.
ourse id
and ID= 12345)
union
(sele
t 0
from student
where ID = 12345 and
not exists ( sele
t * from takes where ID = 12345))
b. Find the grade point average (GPA) for the above student, that is, the total
grade-points divided by the total
redits for the asso
iated
ourses.
sele
t sum(
redits * points)/sum(
redits) as GPA
from takes,
ourse, grade points
where takes.grade = grade points.grade
and takes.
ourse id =
ourse.
ourse id
and ID= 12345
As before, a student who has not taken any
ourse would not appear in
the above result; we
an ensure that su
h a student appears in the result by
using the modied query from the previous part of this question. However,
an additional issue in this
ase is that the sum of
redits would also be 0,
resulting in a divide-by-zero
ondition. In fa
t, the only meaningful way
of dening the GPA in this
ase is to dene it as null. We
an ensure that
su
h a student appears in the result with a null GPA by adding the following
union
lause to the above query.
Pra
ti
e Exer
ises 13
union
(sele
t null as GPA
from student
where ID = 12345 and
not exists ( sele
t * from takes where ID = 12345))
union
(sele
t ID, null as GPA
from student
where not exists ( sele
t * from takes where takes.ID = student.ID))
d. Now re
onsider your answers to the earlier parts of this exer
ise under
the assumption that some grades might be null. Explain whether your
solutions still work and, if not, provide versions that handle nulls properly.
The queries listed above all in
lude a test of equality on grade between
grade points and takes. Thus, for any takes tuple with a null grade, that
student's
ourse would be eliminated from the rest of the
omputation
of the result. As a result, the
redits of su
h
ourses would be eliminated
also, and thus the queries would return the
orre
t answer even if some
grades are null.
3.3
Answer:
b. Delete all
ourses that have never been oered (that is, do not o
ur in
the se
tion relation).
14 Chapter 3 Introdu
tion to SQL
. Insert every student whose tot
red attribute is greater than 100 as an in-
stru
tor in the same department, with a salary of $10,000.
insert into instru
tor
sele
t ID, name, dept name, 10000
from student
where tot
red > 100
3.4
Answer:
a. Find the total number of people who owned
ars that were involved in
a
idents in 2017.
Note: This is not the same as the total number of a
idents in 2017. We
must
ount people with several a
idents only on
e. Furthermore, note
that the question asks for owners, and it might be that the owner of the
ar was not the driver a
tually involved in the a
ident.
sele
t
ount (distin
t person.driver id)
from a
ident, parti
ipated, person, owns
where a
ident.report number = parti
ipated.report number
and owns.driver id = person.driver id
and owns.li
ense plate = parti
ipated.li
ense plate
and year = 2017
Note: The owns, a
ident and parti
ipated re
ords asso
iated with the
deleted
ars still exist.
Pra
ti
e Exer
ises 15
3.5
Answer:
Answer:
3.7
Answer:
The query sele
ts those values of p.a1 that are equal to some value of r1.a1 or
r2.a1 if and only if both r1 and r2 are non-empty. If one or both of r1 and r2 are
16 Chapter 3 Introdu
tion to SQL
empty, the Cartesian produ
t of p, r1 and r2 is empty, hen
e the result of the
query is empty. If p itself is empty, the result is empty.
3.8
Answer:
a. Find the ID of ea
h
ustomer of the bank who has an a
ount but not a
loan.
(sele
t ID
from depositor)
ex
ept
(sele
t ID
from borrower)
b. Find the ID of ea
h
ustomer who lives on the same street and in the same
ity as
ustomer 12345.
sele
t F.ID
from
ustomer as F,
ustomer as S
where F.
ustomer street = S.
ustomer street
and F.
ustomer
ity = S.
ustomer
ity
and S.
ustomer id = 12345
. Find the name of ea
h bran
h that has at least one
ustomer who has an
a
ount in the bank and who lives in Harrison.
sele
t distin
tbran
h name
from a
ount, depositor,
ustomer
where
ustomer.id = depositor.id
and depositor.a
ount number = a
ount.a
ount number
and
ustomer
ity = 'Harrison'
Pra
ti
e Exer
ises 17
3.9
Answer:
a. Find the ID, name, and
ity of residen
e of ea
h employee who works for
First Bank Corporation.
b. Find the ID, name, and
ity of residen
e of ea
h employee who works for
First Bank Corporation and earns more than $10000.
sele
t *
from employee
where ID in
(sele
t ID
from works
where
ompany name = First Bank Corporation and salary > 10000)
sele
t ID
from employee
where ID not in
(sele
t ID
from works
where
ompany name = First Bank Corporation)
sele
t ID
from works
where salary > all
(sele
t salary
from works
where
ompany name = Small Bank Corporation)
If people may work for several
ompanies and we wish to
onsider the total
earnings of ea
h person, the problem is more
omplex. But note that the
fa
t that ID is the primary key for works implies that this
annot be the
ase.
e. Assume that
ompanies may be lo
ated in several
ities. Find the name
of ea
h
ompany that is lo
ated in every
ity in whi
h Small Bank Cor-
poration is lo
ated.
3.10
Answer:
a. Modify the database so that the employee whose ID is 12345 now lives
in Newtown.
update employee
set
ity = Newtown
where ID = 12345
b. Give ea
h manager of First Bank Corporation a 10 per
ent raise unless
the salary be
omes greater than $100000; in su
h
ases, give only a 3
per
ent raise.
update works T
setT.salary = T.salary * 1.03
where T .ID in (sele
t manager id
from manages)
and T.salary * 1.1 > 100000
and T.
ompany name = First Bank Corporation
update works T
setT.salary = T.salary * 1.1
where T .ID in (sele
t manager id
from manages)
and T.salary * 1.1 <= 100000
and T.
ompany name = First Bank Corporation
The above updates would give dierent results if exe
uted in the opposite
order. We give below a safer solution using the
ase statement.
update works T
set T.salary = T.salary <
(
ase
when (T.salary < 1:1 > 100000) then 1.03
else 1.1
end)
where T.ID in (sele
t manager id
from manages) and
T.
ompany name = First Bank Corporation