Number of Unique Subjects Taught by Each Teacher - Problem
Given a Teacher table that tracks which subjects each teacher teaches in different departments, your task is to find how many unique subjects each teacher teaches across the entire university.
The Teacher table has the following structure:
| Column Name | Type |
|---|---|
| teacher_id | int |
| subject_id | int |
| dept_id | int |
Key points:
- The combination
(subject_id, dept_id)is the primary key - A teacher can teach the same subject in multiple departments
- We need to count each subject only once per teacher, regardless of how many departments they teach it in
Goal: Write a SQL query to calculate the number of unique subjects each teacher teaches and return the results as (teacher_id, cnt) where cnt is the count of unique subjects.
Input & Output
example_1.sql โ Basic Case
$
Input:
Teacher table:
+------------+------------+---------+
| teacher_id | subject_id | dept_id |
+------------+------------+---------+
| 1 | 2 | 3 |
| 1 | 2 | 4 |
| 1 | 3 | 3 |
| 2 | 1 | 1 |
| 2 | 2 | 1 |
| 2 | 3 | 1 |
| 2 | 4 | 1 |
+------------+------------+---------+
โบ
Output:
+------------+-----+
| teacher_id | cnt |
+------------+-----+
| 1 | 2 |
| 2 | 4 |
+------------+-----+
๐ก Note:
Teacher 1: Teaches subject 2 in departments 3 and 4 (counts as 1 unique subject), and teaches subject 3 in department 3. Total unique subjects = 2. Teacher 2: Teaches 4 different subjects (1, 2, 3, 4) all in department 1. Total unique subjects = 4.
example_2.sql โ Single Teacher
$
Input:
Teacher table:
+------------+------------+---------+
| teacher_id | subject_id | dept_id |
+------------+------------+---------+
| 5 | 10 | 1 |
| 5 | 10 | 2 |
| 5 | 10 | 3 |
+------------+------------+---------+
โบ
Output:
+------------+-----+
| teacher_id | cnt |
+------------+-----+
| 5 | 1 |
+------------+-----+
๐ก Note:
Teacher 5 teaches the same subject (subject 10) in three different departments (1, 2, 3). Since we count unique subjects per teacher, this counts as only 1 unique subject.
example_3.sql โ No Duplicates
$
Input:
Teacher table:
+------------+------------+---------+
| teacher_id | subject_id | dept_id |
+------------+------------+---------+
| 3 | 1 | 1 |
| 3 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 1 |
+------------+------------+---------+
โบ
Output:
+------------+-----+
| teacher_id | cnt |
+------------+-----+
| 3 | 3 |
| 4 | 1 |
+------------+-----+
๐ก Note:
Teacher 3 teaches 3 different subjects (1, 2, 3) in different departments with no overlaps. Teacher 4 teaches only 1 subject (4). All subjects are already unique per teacher.
Constraints
- 1 โค teacher_id โค 105
- 1 โค subject_id โค 105
- 1 โค dept_id โค 105
- The combination (subject_id, dept_id) is unique (primary key constraint)
- Each teacher teaches at least one subject
Visualization
Tap to expand
Understanding the Visualization
1
Collect Teaching Records
Gather all teaching assignments showing which professor teaches what subject in which department
2
Group by Professor
Organize all records by teacher_id to analyze each professor's portfolio separately
3
Identify Unique Subjects
For each professor, identify their unique subjects using DISTINCT, ignoring department duplicates
4
Count Subject Diversity
Count how many different subjects each professor can teach (their teaching versatility)
5
Generate Portfolio Report
Output each professor's ID and their unique subject count as the final diversity report
Key Takeaway
๐ฏ Key Insight: This problem measures teaching versatility (how many different subjects a professor can teach) rather than teaching workload (total classes taught). The DISTINCT keyword in SQL automatically handles the complexity of identifying unique subjects across multiple departments.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code