25-Comp-B3 Data Bases and File Systems · May 2015
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Given. Student(Id,Name,Country), Course(CrsCode,CrsName,Type,Instructor), Results(Id,CrsCode,Grade); Type classifies each course (e.g. MATH, STAT, SYSC, TTMG, ELEC).
Find. (a) students in TTMG or SYSC; (b) students in every course; (c) students who complete every SYSC course OR every TTMG course; (d) a plain-English reading of a given relational-algebra query.
Approach. (a) is a simple join+filter with OR; (b) and (c) are relational-division patterns like Question 5(a)(ii), expressed here directly in SQL; (d) is read inside-out: innermost selection/projection first, then each join, then the outer projection.
SELECT DISTINCT r.Id
FROM Results r JOIN Course c ON r.CrsCode = c.CrsCode
WHERE c.Type = 'TTMG' OR c.Type = 'SYSC';SELECT s.Id FROM Student s
WHERE NOT EXISTS (
SELECT c.CrsCode FROM Course c
WHERE NOT EXISTS (
SELECT 1 FROM Results r WHERE r.Id = s.Id AND r.CrsCode = c.CrsCode
)
);SELECT s.Id FROM Student s
WHERE NOT EXISTS (
SELECT c.CrsCode FROM Course c WHERE c.Type = 'SYSC'
AND NOT EXISTS (SELECT 1 FROM Results r WHERE r.Id = s.Id AND r.CrsCode = c.CrsCode)
)
OR NOT EXISTS (
SELECT c.CrsCode FROM Course c WHERE c.Type = 'TTMG'
AND NOT EXISTS (SELECT 1 FROM Results r WHERE r.Id = s.Id AND r.CrsCode = c.CrsCode)
);
AND EXISTS (SELECT 1 FROM Course c WHERE c.Type='SYSC') (or the TTMG equivalent) to rule out the zero-course case.σType='SYSC'Course selects only SYSC courses, and πCrsCode reduces that to just their course codes. Joining that against σGrade='D'Result (natural join on the shared CrsCode column) keeps only D-grade result rows whose course is one of those SYSC codes. Joining THAT against Student (natural join on Id) attaches each surviving result to its student, and the outer πName keeps just the name. In plain English: the query returns the names of students who received a grade of D in at least one SYSC course.| Part | Result |
|---|---|
| (a) | students with ≥1 TTMG or ≥1 SYSC result |
| (b) | students whose Results cover the ENTIRE Course table |
| (c) | students covering all SYSC courses OR all TTMG courses |
| (d) | "names of students who got a D in some SYSC course" |