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

Indiana University Database Homework Solutions

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)
11 views2 pages

Indiana University Database Homework Solutions

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

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([Link] = [Link] ∧ [Link] = [Link] ∧ ∀a ∈ T ake([Link] =


[Link] ∧ ([Link] = “A + ” ∨ [Link] = “A” ∨ [Link] = “A − ”)))}
SQL: SELECT [Link],[Link] FROM Students as S,Take as T
WHERE [Link] = [Link]
EXCEPT
SELECT [Link],[Link] FROM Students as S,Take as T
WHERE [Link] = [Link] and [Link]!=’A+’ and
[Link]!=’A’ and [Link]!=’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([Link] = [Link] ∧ ∀a ∈ T aken([Link] = [Link] ∧
∃r ∈ RequiredCourse([Link] = [Link] ∧ [Link] = [Link])))}
SQL: SELECT [Link] FROM Students as St
EXCEPT
SELECT [Link] FROM (SELECT [Link],[Link] FROM Take as T
EXCEPT
SELECT [Link],[Link] FROM Students as S,RequiredCourse as R
WHERE [Link] = [Link]) 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 [Link]=[Link])
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 [Link],[Link] FROM Students as S1 WHERE GPA >2 and


NOT EXISTS(
SELECT [Link],[Link] Students as S,RequiredCourse as RC
WHERE [Link] = [Link] and [Link]=[Link]

1
INTERSECT
SELECT [Link],[Link] FROM Take as T,RequiredCourse as RC
WHERE [Link] = [Link] and [Link]=[Link]) and
EXISTS(
SELECT SUM([Link]) FROM Students as S,Take as T, Courses as C
WHERE [Link] = [Link] and [Link]=[Link] and [Link] = [Link]
GROUP BY [Link]
HAVING SUM([Link])>=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 [Link],[Link] FROM Courses as C,RequiredCourse as RC


WHERE [Link]=[Link]
GROUP BY [Link] HAVING COUNT([Link])>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.

Common questions

Powered by AI

In SQL, to ensure students' eligibility for graduation based on course completion, GPA, and credit hour requirements, the strategy involves using logical operators, subqueries, and aggregation functions. The query checks for GPA greater than 2.0, non-existence of non-completed required courses through an INTERSECT operator, and a EXISTS condition to check credit hours using SUM and HAVING functions for at least 90 credits: SELECT S1.sid, S1.name FROM Students as S1 WHERE GPA > 2 and NOT EXISTS (SELECT S.sid, RC.cid FROM Students as S, RequiredCourse as RC WHERE S.dept = RC.dept and S.sid = S1.sid 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)

To use relational algebra to find students who haven’t taken non-required courses, the strategy involves using the difference operation to subtract a set of non-required courses from the student's records. The process starts with forming a set of student-course pairs for courses taken and subtracting from this the set of student-course pairs for courses not required. The expression is πsid(Student) − πsid(πsid,cid(Take) − πsid,cid(Students ▷◁ RequiredCourses)). This operation effectively isolates students who only took required courses.

Set operations like EXCEPT in SQL and difference operations in relational algebra both aim to find exclusive data sets by removing elements present in one set from another. In SQL, EXCEPT is used between two select queries and naturally operates on rows, ensuring only unique results not appearing in the second set are returned. In relational algebra, the difference is explicit in representing subtraction of relations and follows a theoretical model. Both methods serve similar purposes but apply over different syntactical and operational paradigms.

To find students who have only taken courses required by their department, an EXCEPT statement is used to subtract unrelated taken courses from the student records. The SQL query retrieves student IDs of those who have not taken any non-required courses by using a nested EXCEPT operation within the main EXCEPT operation: 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. This identifies students who have only taken required courses.

A course can be identified as required by multiple departments by using a GROUP BY and HAVING clause in SQL to count how many departments specify it as a requirement. In the given example, the threshold for identification is set at more than 10 departments. The SQL query is: 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. This counts appearances of course IDs and identifies those exceeding the threshold.

For a student to be eligible for graduation from their department, they must meet several conditions: complete at least 90 credit hours, maintain an overall GPA greater than 2.0, and fulfill all required courses for their department. The SQL query checks these conditions using logical operators and subqueries to ensure no required course is missing and that the summed credits meet or exceed 90: SELECT S1.sid, S1.name FROM Students as S1 WHERE GPA > 2 and NOT EXISTS (SELECT S.sid, RC.cid FROM Students as S, RequiredCourse as RC WHERE S.dept = RC.dept and S.sid = S1.sid 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)

Subqueries play a critical role in managing complex conditions by allowing nested queries to provide data that the main query can use to filter or compute results. They enable breaking down extensive problems into smaller queries that then supply the necessary conditions or results used by the outer query. This is evident in queries that calculate maximum values, verify existence with logical comparisons, or aggregate metrics like GPA or credit hours to meet specific conditions.

To determine the top-performing students by GPA in each department, you can use a subquery to find the maximum GPA within each department and then select students having that GPA. The SQL query is: SELECT name, dept, GPA FROM Students as S1 WHERE GPA = (SELECT max(GPA) FROM Students as S2 WHERE S1.dept = S2.dept). This query matches each student's GPA to the maximum GPA within their respective department, thereby identifying the top students.

EXISTS and NOT EXISTS conditions are used in SQL to enhance data validation by ensuring particular states or entities exist or do not exist within the specified subset of data. EXISTS checks whether the subquery returns any rows, making it useful for conditional checks. NOT EXISTS is employed inversely to ascertain that a condition doesn’t hold by confirming the absence of matching records. They are particularly valuable when validating comprehensive conditions like checking fulfillment of course requirements or ensuring credit thresholds without having to enumerate precise details inline in the main query.

To identify students who have never received a grade lower than A-, the SQL query uses two SELECT statements with an EXCEPT operation. The first SELECT retrieves all students with their grades, while the second SELECT identifies students who have grades lower than A-. By subtracting the second set from the first, only those students who never received a grade lower than A- are identified: 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.grade != 'A+' and T.grade != 'A' and T.grade != 'A-'.

You might also like