Assignment 04
Assignment 04
1. Suppose you are given a relation grade points(grade, points), which provides a conversion
from letter grades in the takes relation to numeric scores; for example, an “A” grade could
be specified to correspond to 4 points, an “A−” to 3.7 points, a “B+” to 3.3 points, a “B”
to 3 points, and so on. The grade points earned by a student for a course offering (section)
is defined as the number of credits for the course multiplied by the numeric points for the
Given the above relation, and our university schema, write each of the following
queries in SQL. You can assume for simplicity that no takes tuple has the null value for
grade.
a. Find the total grade-points earned by the student with ID 12345, across all courses
b. Find the grade-point average (GPA) for the above student, that is, the total grade-
whereID = ’12345’
One problem with the above query is that if the student has not
taken any course, the result would not have any tuples, whereas we
would expect to get 0 as the answer. One way of fixing this problem
is to use the natural left outer join operation, which we study later
where ID = ’12345’)
union
(select 0
from student
clause instead of using the natural join operation or the join .. using
operation.
Exercises 9
b. Find the grade-point average (GPA) for the above student, that is,
the total grade-points divided by the total credits for the associated
courses.
where ID = ’12345’
As before, a student who has not taken any course would not appear
in the above result; we can ensure that such a student appears in the
result by using the modified query from the previous part of this
In fact, the only meaningful way of defining the GPA in this case is
result with a null GPA by adding the following union clause to the
above query.
union
from student
Other ways of ensuring the above are discussed later in the solution
to Exercise 4.5.
group by ID
Again, to handle students who have not taken any course, we would
union
from student