Count Each Teacher’s Distinct Subjects across Departments
Company: Google
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
### Problem
Return teacher_id and cnt, the number of different subject_id values taught by that teacher. Teaching the same subject in several departments counts once. The source key is (subject_id,dept_id), and result row order is unrestricted.
### Tables
Table: `Teacher`
```text
+-------------+------+
| Column Name | Type |
+-------------+------+
| teacher_id | int |
| subject_id | int |
| dept_id | int |
+-------------+------+
(subject_id, dept_id) is the primary key (combinations of columns with unique values) of this table.
Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id.
```
### Example
```text
Teacher (teacher_id,subject_id,dept_id): (1,8,2), (1,8,3), (1,9,2), (2,7,2)
Output (teacher_id,cnt): (1,2), (2,1)
```
### What a Strong Answer Covers
- Count each subject once for a teacher, even if the teacher covers it in several departments.
- Return the requested count for each teacher in the table.
### Follow-up Questions
- How would you report both distinct subjects and distinct departments?
- How would you include teachers with no assignments if another table listed all teachers?
Overview: Count the distinct subjects taught by each teacher, even when a subject appears across departments.