SQL Query Exercises.doc
SQL Query Exercises.doc
b. Find the IDs of all students who were taught by an instructor named Einstein; make sure
there are no duplicates in the result.
SELECT DISTINCT student.ID
From (student join takes using(ID))
using(course_id, sec_id, semester, year)
WHERE instructor.name = ‘Einstein’
e. Find the enrollment of each section that was offered in the Fall 2009.
g. Find the sections that had the maximum enrollment in Fall 2009.
2. Suppose you are given a relation grad_poins(grade, points) which provides a conversion from letter
grades in the takes relation to numeric score; for example an “A” grade could be specified to correspond
to 4 points , and “A-” to 3.7 points, a “ B+” to 3.3 points, a “B” to 3 points, and so on. The grades 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 grade that the student received.
Give 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 null value for grade.
a. Find the total grade-points earned by the student with ID 12345, across all course taken by the
student.
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.
c. Find the ID and the grade-point average of every student.
3. Write the following inserts, deletes or updates in SQL, using the university schema.
a. Increase the salary of each instructor in the Comp.Sci. department by 10%.
b. Delete all courses that have never been offered. (that is, do not occur in the section relation)
c. Insert every student whose tot_cred attribute is greater than 100 as an instructor in the same
department, with a salary of $10,000.
4. Suppose that we have a relation marks(ID, score) and we wish to assign grades to students based on
the score as follows: grade F i score < 40, grade C if 40<= score < 60, grade B if 60<= score< 80, and grade
A if 80<=score. Write SQL queries to do the following:
a. Display the grade for each student, based on the marks relation.
b. Find the number of students with each grade.