Compute each student's exam attendance across multiple subjects, including subjects with zero attempts.
Students and Examinations
You are given three tables describing students, subjects, and examination records. For every student, report how many times they took each subject exam.
A student should appear once for every subject, even if they never took that exam. The result should include the student's ID, name, subject name, and the number of attended exams.
Return the rows ordered by student ID, then subject name.
Input Format
- A table of students with columns such as
student_idandstudent_name. - A table of subjects with columns such as
subject_name. - A table of examination records with columns such as
student_idandsubject_name.
Each row in the exam table represents one exam attendance.
Output Format
Return one row per (student, subject) pair with:
- student ID
- student name
- subject name
- attended exam count
Constraints
- Every student must be paired with every subject.
- Counts may be zero.
- Sorting should be deterministic by student ID and subject name.
- Assume input tables may contain multiple exam rows for the same student and subject.
Example 1
Input
Students | student_id | student_name | |-----------:|--------------| | 1 | Alice | | 2 | Bob | Subjects | subject_name | |--------------| | Math | | Physics | Examinations | student_id | subject_name | |-----------:|--------------| | 1 | Math | | 1 | Math | | 1 | Physics | | 2 | Math |
Output
| student_id | student_name | subject_name | attended_exams | |-----------:|--------------|--------------|----------------:| | 1 | Alice | Math | 2 | | 1 | Alice | Physics | 1 | | 2 | Bob | Math | 1 | | 2 | Bob | Physics | 0 |
Explanation
Generate all student-subject combinations, then count how many examination rows match each pair. Bob never took Physics, so his count is 0.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.