I have a question about an SQL I want to perform but got no idea on how to write it. Below are the tables with so dummy data:
Module:
ModuleId (PK auto) ModuleNo ModuleName
1 CHI2523 Business
Course:
CourseId (PK auto) CourseName
1 Business and Computing
2 ICT
3 Sports
Course_Module:
CourseId (PK) ModuleId (PK)
1 1
2 1
Session:
SessionId (PK auto) SessionName ModuleId (FK)
3 DFRER 1
Student:
StudentId (PK auto) StudentAlias StudentForename StudentSurname CourseId (FK)
1 u03824 Bill Murphy 1
2 u38492 Conrad Jones 2
3 u48383 Jane Peters 1
4 u34322 Morgan Gray 2
5 u39292 Bilal Tuddy 3
Student_Session:
SessionId (PK) StudentId (PK)
3 1
3 2
What I want the query to be able to find is all of the remaining students that are not currently taking the selected assessment but who are legible to take the assessment.
So to be able to do this we have to:
-
Look at what the selected session is by looking up
SessionIdinSessionTable -
See which module that
SessionIdbelongs to by seeing theModuleIdinSessionTable. -
See which courses that
ModuleIdis in by looking upCourse_Moduletable -
Look up each student’s course by looking at
CourseIdin student table that matches with theCourseId‘s from theCourse_ModuleTable -
Display all students who belongs to the Course where the module is in, but only dislay students who are not currently in the session (or in other words not in the
SessionTable taking the selectedSessionId.
So if the SessionId chosen is 3, then the students it should output are:
StudentId (PK auto) StudentAlias StudentForename StudentSurname CourseId (FK)
3 u48383 Jane Peters 1
4 u34322 Morgan Gray 2
This is because:
-
SessionId3 belongs toModuleId1 -
ModuleIdbelongs toCourseId1 and 2 -
Looking up
CourseId1 and 2 there are 4 students who take that course -
But 2 of those students are already taking
SessionId3 when looking atStudent_SessionTable, so only display the other 2 students as they are currently not taking the session
My question is how is the query suppose to be written to be able to achieve the example mentioned above? I have made a start on the query but need someone’s help to enhance it so it meets with the repuirements of the example above:
SELECT s.StudentId, StudentAlias, StudentForename, StudentSurname
FROM Session s
INNER JOIN Module m ON m.ModuleId = s.ModuleId
INNER JOIN Course_Module cm ON cm.ModuleId = m.ModuleId
INNER JOIN Student s ON cm.CourseId = s.CourseId
INNER JOIN Student_Session ss ON s.StudentId = ss.StudentId
WHERE s.SessionId = 3
ORDER BY StudentAlias;
I don’t know how to check to see which students are/are not legible and then selecting only those students who are legible for taking assessment but who are not currently taking part in assessment
1 Answer