denote algebra

advertisement
``SID ______________
(40 minutes)
Last Name __________ First Name_________ In-class quiz
Consider the following relations and use relational algebra to answer all five questions.
Students(sid: integer, sname: string, age: real, state: string)
Courses(cid: integer, cname:string, credit: integer)
Enroll(sid:integer, cid: integer, semester:string, grade:real)
You can use S, C, and E to denote the above three tables respectively in your relational algebra
expression.
1. (1pts) Find the student names who are from NC and with age above 18.
SELECT S.sname FROM Student S WHERE S.state =”NC” and S.age > 18
2. (1pts)Find the student names who enrolled 5160 course in Spring 10
SELECT S.sname FROM Student S, Enroll E WHERE S.sid = E.sid and E.cid = 5160 and
E.semester = “Spring 10”
3. (2pts)Find the student names who did not enroll 5160 course in Spring 10
SELECT S.sname DROM Student S WHERE S.sid NOT IN (SELECT E.Sid DROM Enroll E
WHERE E.cid = 5160 and E.semester = “Spring 10”)
4. (2pts) Find the student names who have taken all 5000 level courses before Fall 09
SELECT S.sname
FROM Student S
WHERE NOT EXIST (
SELECT C.cid FROM Course C WHERE C.cid >=5000 AND C.cid<6000
EXCEPT
SELECT E.cid FROM Enroll E WHERE E.sid = S.sid AND E.semester < “Fall 09”
)
5. (2pts)For each course with more than 20 students, list the average grade of students.
SELECT E.cid, AVG(E.grade)
FROM Enroll E
GROUP BY E.cid
HAVING COUNT(*) > 20
6. (2pts)For each course, list the average grade of students from each state.
SELECT E.cid, S.state, AVG(E.grade)
FROM Student S, Enroll E
WHERE S.sid = E.sid
GROUP BY E.cid, S.state
Download