Students and Examinations - Problem

You work for a university's academic records system. The school has students enrolled in various subjects, and students take examinations throughout the semester.

Given three database tables:

  • Students: Contains student information (ID and name)
  • Subjects: Contains all available subjects in the school
  • Examinations: Records every time a student takes an exam (may contain duplicates since students can retake exams)

Your task is to create a comprehensive report showing how many times each student attended each exam, including subjects where a student attended zero exams.

The result should be ordered by student_id and subject_name.

Example: If Alice is enrolled and there are subjects Math and Physics, but she only took 2 Math exams and 0 Physics exams, your report should show both entries with counts 2 and 0 respectively.

Input & Output

example_1.sql โ€” SQL Query
$ Input: Students = [[1,"Alice"],[2,"Bob"],[13,"John"],[6,"Alex"]] Subjects = [["Math"],["Physics"],["Programming"]] Examinations = [[1,"Math"],[1,"Physics"],[1,"Programming"],[2,"Programming"],[1,"Physics"],[1,"Math"],[13,"Math"],[13,"Programming"],[13,"Physics"],[2,"Math"],[1,"Math"]]
โ€บ Output: [[1,"Alice","Math",3],[1,"Alice","Physics",2],[1,"Alice","Programming",1],[2,"Bob","Math",1],[2,"Bob","Physics",0],[2,"Bob","Programming",1],[6,"Alex","Math",0],[6,"Alex","Physics",0],[6,"Alex","Programming",0],[13,"John","Math",1],[13,"John","Physics",1],[13,"John","Programming",1]]
๐Ÿ’ก Note: Alice took Math 3 times, Physics 2 times, Programming 1 time. Bob took Math and Programming 1 time each, but never took Physics (0). Alex never took any exams (all 0s). John took each subject exactly once.
example_2.sql โ€” Edge Case
$ Input: Students = [[1,"Alice"]] Subjects = [["Math"],["Physics"]] Examinations = []
โ€บ Output: [[1,"Alice","Math",0],[1,"Alice","Physics",0]]
๐Ÿ’ก Note: Even when no examinations exist, we still need to show all student-subject combinations with 0 counts. This demonstrates the importance of CROSS JOIN.
example_3.sql โ€” Single Student Multiple Exams
$ Input: Students = [[1,"Alice"]] Subjects = [["Math"]] Examinations = [[1,"Math"],[1,"Math"],[1,"Math"]]
โ€บ Output: [[1,"Alice","Math",3]]
๐Ÿ’ก Note: Alice took the Math exam 3 times (retakes are allowed), so the count is 3.

Visualization

Tap to expand
School Report Card SystemStep 1: Create GridStudent โ†’ SubjectAlice โ†’ Math: ___Alice โ†’ Physics: ___Bob โ†’ Math: ___Step 2: Count ExamsExam Records(Alice, Math) โ†’ 2(Bob, Physics) โ†’ 1Step 3: Fill GridComplete ReportAlice โ†’ Math: 2Alice โ†’ Physics: 0Bob โ†’ Math: 0SQL ImplementationSELECT s.student_id, s.student_name, sub.subject_name, COALESCE(e.count, 0) as attendedFROM Students sCROSS JOIN Subjects subLEFT JOIN (exam counts) eORDER BY s.student_id, sub.subject_name๐ŸŽฏ Key Insight: CROSS JOIN creates the complete grid, LEFT JOIN fills in the counts!
Understanding the Visualization
1
Create Master Grid
Like creating an empty report card template with every student name and every subject listed
2
Count Exam Attempts
Go through exam records and tally how many times each student attempted each subject
3
Fill the Grid
Fill in the counts on the report card, writing 0 for subjects never attempted
4
Sort and Present
Organize the report cards by student ID and subject name for easy reading
Key Takeaway
๐ŸŽฏ Key Insight: The magic happens with CROSS JOIN creating all possible student-subject combinations, then LEFT JOIN with COALESCE ensuring zero counts are properly displayed for comprehensive reporting.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(S + Su + E + (Sร—Su) log (Sร—Su))

Reading tables plus sorting final result - database engines optimize joins efficiently

n
2n
โšก Linearithmic
Space Complexity
O(S ร— Su)

Final result contains all student-subject combinations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค students.length โ‰ค 1000
  • 1 โ‰ค subjects.length โ‰ค 100
  • 0 โ‰ค examinations.length โ‰ค 104
  • 1 โ‰ค student_id โ‰ค 1000
  • 1 โ‰ค student_name.length, subject_name.length โ‰ค 20
  • Each student_id in examinations exists in students table
  • Each subject_name in examinations exists in subjects table
Asked in
Google 23 Amazon 19 Microsoft 15 Meta 12
28.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen