NivaarExam PrepOfficial exam papers ↗

25-Comp-B3 Data Bases and File Systems · December 2014

Question 6 of 8: SQL & relational algebra — Student/Course/Results

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

98-Comp-B3, Data Bases & File Systems — National Exams, December 2014. 3 hours, closed book (calculators permitted). Candidates were instructed to answer five questions: one of Questions 1/2, one of Questions 3/4, and three of Questions 5–8 — only those five are marked. All 8 questions are answered below for completeness (this is a study resource covering the full syllabus).

Check: the source page header prints “98-Comp-B3/ December 2014” while the title block prints “National Exams May 2014” — a date inconsistency on the printed cover page. This is treated as the December 2014 exam period, matching every subsequent page header. Also, the page-1 marking scheme lists Question 8 as having two part-(c) entries (“(c) 4 marks; (c) 6 marks”); read as a mislabelled (c)/(d), matching the body text's actual four sub-parts (a)(b)(c)(d).

Reference texts: Silberschatz, Korth & Sudarshan, Database System Concepts (7th ed.) — RAID, indexing, ER modelling, normal forms, transactions and serializability; Ramakrishnan & Gehrke, Database Management Systems (3rd ed.) — B+-tree indexing, SQL, relational algebra, and concurrency control.

Question 6: SQL & relational algebra — Student/Course/Results (20 marks)

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), with Results.Id/CrsCode foreign keys into Student/Course.

(a) Id of students who take TTMG or SYSC courses — join Results to Course on CrsCode, filter by course Type.

SELECT DISTINCT R.Id
FROM   Results R, Course C
WHERE  R.CrsCode = C.CrsCode AND C.Type IN ('TTMG', 'SYSC');

(b) Id of students who take EVERY course — the classic universal-quantification pattern via double negation: no course exists that this student has NOT taken.

SELECT S.Id
FROM   Student S
WHERE  NOT EXISTS (
         SELECT * FROM Course C
         WHERE  NOT EXISTS (
                  SELECT * FROM Results R
                  WHERE  R.Id = S.Id AND R.CrsCode = C.CrsCode));

(c) Id of students who take every SYSC course OR take every TTMG course — the same double-negation pattern, restricted to one course Type at a time, unioned.

SELECT S.Id FROM Student S
WHERE  NOT EXISTS (
         SELECT * FROM Course C WHERE C.Type = 'SYSC'
         AND    NOT EXISTS (SELECT * FROM Results R
                             WHERE R.Id = S.Id AND R.CrsCode = C.CrsCode))
UNION
SELECT S.Id FROM Student S
WHERE  NOT EXISTS (
         SELECT * FROM Course C WHERE C.Type = 'TTMG'
         AND    NOT EXISTS (SELECT * FROM Results R
                             WHERE R.Id = S.Id AND R.CrsCode = C.CrsCode));

(d) Plain-English reading of the relational-algebra query. Working inside-out: $\sigma_{Type='SYSC'}(Course)$ is every SYSC course row; projecting $\pi_{CrsCode}$ of that keeps just their course codes. $\sigma_{Grade='D'}(Result)$ is every (Id, CrsCode) result row where the grade was a D. Natural-joining these two (on the shared CrsCode attribute) keeps only the D-grade result rows whose course is one of the SYSC courses. Natural-joining that against Student (on the shared Id attribute) attaches each surviving row's student Name (and Country). The final $\pi_{Name}$ keeps only the Name column.

In plain English: "the names of students who received a grade of D in some SYSC course."

Sample-data check — Question 6
QueryResult on the sample dataset
(a)Ids 1, 2, 3
(b)Ids 1, 3
(c)Ids 1, 2, 3 (Id 2 satisfies "every TTMG course" — only one TTMG course exists)
(d){Aiko} (Aiko received a D in SYSC2000)