0% found this document useful (0 votes)
6 views2 pages

AdvancedDatabaseConcepts-Assignment-2-Solution

The document contains homework questions and SQL queries related to advanced database concepts at Indiana University. It includes tasks such as finding students with high grades, those who have only taken required courses, identifying students with the highest GPA in each department, and determining eligibility for degrees based on credit hours and GPA. Additionally, there is a bonus question about courses required by multiple departments.

Uploaded by

Phan Duc Tri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

AdvancedDatabaseConcepts-Assignment-2-Solution

The document contains homework questions and SQL queries related to advanced database concepts at Indiana University. It includes tasks such as finding students with high grades, those who have only taken required courses, identifying students with the highest GPA in each department, and determining eligibility for degrees based on credit hours and GPA. Additionally, there is a bonus question about courses required by multiple departments.

Uploaded by

Phan Duc Tri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

INDIANA UNIVERSITY

B561 Advanced Database Concepts


Enrique Areyan

Homework 2

(Q1) Find the sids and names of all students who never got a grade lower than A- (A- is OK).

RA: πsid,name (Students ./ T ake) − πsid,name (Students ./grade!=“A+”∧ T ake)


grade!=“A”∧
grade!=“A−”

RC: {s ∈ Students|∃t ∈ Students(s.sid = t.sid ∧ s.name = t.name ∧ ∀a ∈ T ake(a.sid =


t.sid ∧ (a.grad = “A + ” ∨ a.grade = “A” ∨ a.grade = “A − ”)))}
SQL: SELECT S.sid,S.name FROM Students as S,Take as T
WHERE S.sid = T.sid
EXCEPT
SELECT S.sid,S.name FROM Students as S,Take as T
WHERE S.sid = T.sid and T.grad!=’A+’ and
T.grad!=’A’ and T.grad!=’A-’

Q2 Find the students who have never taken any course that is not required by his/her department

RA: πsid (Student) − πsid (πsid,cid (T ake) − πsid,cid (Students ./ RequiredCourses))


RC: {t ∈ Students|∃s ∈ Students(s.sid = t.sid ∧ ∀a ∈ T aken(a.sid = s.sid ∧
∃r ∈ RequiredCourse(r.dept = s.dept ∧ a.cid = r.cid)))}
SQL: SELECT St.sid FROM Students as St
EXCEPT
SELECT St.sid FROM (SELECT T.sid,T.cid FROM Take as T
EXCEPT
SELECT S.sid,R.cid FROM Students as S,RequiredCourse as R
WHERE S.dept = R.dept) as St

Q3 For each department, find the name of the students with the highest GPA (if more than one
student has the same GPA, return them all)

SQL: SELECT name,dept,GPA FROM Students as S1


WHERE GPA = (SELECT max(GPA) FROM Students as S2 WHERE S1.dept=S2.dept)
For each department, as stored in the Student’s table, we calculate the max GPA and
test if the student’s GPA matches the max GPA. If it does, the query returns the student.

Q4 A student can get the degree from his/her department if he/she has taken a total of 90 credit
hours’ courses, has a overall GPA over 2.0, and has taken all required courses. Please find the
sids and names of the students who can get the degree from their department.

SQL: SELECT S1.sid,S1.name FROM Students as S1 WHERE GPA >2 and


NOT EXISTS(
SELECT S.sid,RC.cidFROM Students as S,RequiredCourse as RC
WHERE S.dept = RC.dept and S.sid=S1.sid

1
INTERSECT
SELECT T.sid,T.cid FROM Take as T,RequiredCourse as RC
WHERE S1.sid = T.sid and T.cid=RC.cid) and
EXISTS(
SELECT SUM(C.credit) FROM Students as S,Take as T, Courses as C
WHERE S1.sid = S.sid and S.sid=T.sid and T.cid = C.cid
GROUP BY S.sid
HAVING SUM(C.credit)>=90)
A student will be returned if GPA is greater than two; and does not exists any recored
in the intersection between the required courses of the student’s department and the
required courses the student has taken; and we can calculate the student’s sum of courses’
credits to be greater or equal than 90.

Q5 (bonus) Find the courses (cid and name) that are required by more than 10 departments for
its degree program.

SQL: SELECT C.cid,C.name FROM Courses as C,RequiredCourse as RC


WHERE C.cid=RC.cid
GROUP BY RC.cid HAVING COUNT(RC.dept)>10
The query groups the required courses by the course id and counts the dept column for
each group. If the count is more than 10, the course’s id and name is returned.

You might also like