Count Exam Attendance for Every Student-Subject Pair

Quick Overview

Report exam attendance for every student and subject combination, using zero when no records match.

Count Exam Attendance for Every Student-Subject Pair

Company: Google

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Return student_id, student_name, subject_name and attended_exams for every combination of a Students row and a Subjects row. Count all matching Examinations rows, including repeated attendance records, and show 0 for pairs with no attendance. Sort by student_id then subject_name. ### Tables Table: `Students` ```text +---------------+---------+ | Column Name | Type | +---------------+---------+ | student_id | int | | student_name | varchar | +---------------+---------+ student_id is the primary key (column with unique values) for this table. Each row of this table contains the ID and the name of one student in the school. ``` Table: `Subjects` ```text +--------------+---------+ | Column Name | Type | +--------------+---------+ | subject_name | varchar | +--------------+---------+ subject_name is the primary key (column with unique values) for this table. Each row of this table contains the name of one subject in the school. ``` Table: `Examinations` ```text +--------------+---------+ | Column Name | Type | +--------------+---------+ | student_id | int | | subject_name | varchar | +--------------+---------+ There is no primary key (column with unique values) for this table. It may contain duplicates. Each student from the Students table takes every course from the Subjects table. Each row of this table indicates that a student with ID student_id attended the exam of subject_name. ``` ### Example ```text Students (student_id,student_name): (1,Ada), (2,Bo) Subjects (subject_name): (Art), (Math) Examinations (student_id,subject_name): (1,Art), (1,Art), (2,Math) Output (student_id,student_name,subject_name,attended_exams): (1,Ada,Art,2), (1,Ada,Math,0), (2,Bo,Art,0), (2,Bo,Math,1) ``` ### What a Strong Answer Covers - Return every student and subject pair, including pairs with no attendance. - Count each matching attendance record and sort by student ID and subject name. ### Follow-up Questions - How would you count distinct exam sessions if the records included session IDs? - How would you restrict results to subjects that are offered to each student?

Overview: Report exam attendance for every student and subject combination, using zero when no records match.

Google logo
Google
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Return student_id, student_name, subject_name and attended_exams for every combination of a Students row and a Subjects row. Count all matching Examinations rows, including repeated attendance records, and show 0 for pairs with no attendance. Sort by student_id then subject_name.

Tables

Table: Students

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student_id    | int     |
| student_name  | varchar |
+---------------+---------+
student_id is the primary key (column with unique values) for this table.
Each row of this table contains the ID and the name of one student in the school.

Table: Subjects

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| subject_name | varchar |
+--------------+---------+
subject_name is the primary key (column with unique values) for this table.
Each row of this table contains the name of one subject in the school.

Table: Examinations

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| student_id   | int     |
| subject_name | varchar |
+--------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each student from the Students table takes every course from the Subjects table.
Each row of this table indicates that a student with ID student_id attended the exam of subject_name.

Example

Students (student_id,student_name): (1,Ada), (2,Bo)
Subjects (subject_name): (Art), (Math)
Examinations (student_id,subject_name): (1,Art), (1,Art), (2,Math)
Output (student_id,student_name,subject_name,attended_exams): (1,Ada,Art,2), (1,Ada,Math,0), (2,Bo,Art,0), (2,Bo,Math,1)

What a Strong Answer Covers Guidance

  • Return every student and subject pair, including pairs with no attendance.
  • Count each matching attendance record and sort by student ID and subject name.

Follow-up Questions Guidance

  • How would you count distinct exam sessions if the records included session IDs?
  • How would you restrict results to subjects that are offered to each student?