Question 1Write SQL DDL for Student(StudentID INTEGER primary key, Name VARCHAR(30)) and Score(StudentID INTEGER not null foreign key to Student, Mark INTEGER). Multiple scores per student are allowed. Write a two-table INNER JOIN query returning StudentID, Name and average Mark per student, ordered by StudentID. Students are(1,Ada),(2,Bo),(3,Cy); scores are(1,60),(1,80),(2,90). State query rows and why Cy is absent. Explain why Score.StudentID alone must not be its primary key.6 indicative marks
Write SQL DDL for Student(StudentID INTEGER primary key, Name VARCHAR(30)) and Score(StudentID INTEGER not null foreign key to Student, Mark INTEGER). Multiple scores per student are allowed. Write a two-table INNER JOIN query returning StudentID, Name and average Mark per student, ordered by StudentID. Students are(1,Ada),(2,Bo),(3,Cy); scores are(1,60),(1,80),(2,90). State query rows and why Cy is absent. Explain why Score.StudentID alone must not be its primary key.
Show indicative guidance
Worked guidance
CREATE TABLE Student ( StudentID INTEGER PRIMARY KEY, Name VARCHAR(30) ); CREATE TABLE Score ( StudentID INTEGER NOT NULL, Mark INTEGER, FOREIGN KEY (StudentID) REFERENCES Student(StudentID) ); SELECT Student.StudentID, Student.Name, AVG(Score.Mark) FROM Student INNER JOIN Score ON Student.StudentID = Score.StudentID GROUP BY Student.StudentID, Student.Name ORDER BY Student.StudentID;- The join has three matched score rows: Ada60,Ada80,Bo90. Grouping produces Ada's mean(60+80)/2=70 and Bo's90. Cy contributes no matched row, so no invented zero mark or third row is produced.
- The foreign key prevents a non-null Score.StudentID referencing a missing student. A primary key on that column alone would reject Ada's second score. A complete design may add a separate ScoreID/composite attempt key, but this task deliberately permits repeated student references and does not require an outside-subset LEFT JOIN.
Self-marking points
View marking points
- Create Student with an INTEGER primary key and appropriately sized name field.
- Create Score with NOT NULL StudentID and FOREIGN KEY REFERENCES Student(StudentID).
- Do not make Score.StudentID unique because several scores share it.
- INNER JOIN on matching StudentID and group by both selected nonaggregate fields.
- Use AVG(Mark) and order by StudentID.
- Output(1,Ada,70),(2,Bo,90); unmatched Cy has no INNER JOIN row.
Examiner tip
Indicative distinct points, not an official Cambridge mark allocation. State bounds, assumptions and intermediate states; practical teacher review remains required.