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 NameType
teacher_idint
subject_idint
dept_idint

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
๐ŸŽ“ University Teaching Portfolio Diversity AnalysisP1Professor 1EngCalcMathCalcEngPhysStep 1: Raw Teaching RecordsProfessor 1 โ†’ Calculus (Engineering Dept)Professor 1 โ†’ Calculus (Math Dept) [DUPLICATE]Professor 1 โ†’ Physics (Engineering Dept)Raw Count: 3 recordsStep 2: After DISTINCT ProcessingCCalculus (Unique Subject)PPhysics (Unique Subject)Unique Count: 2 subjectsP2Professor 2CSAlgoDBMLCSStats๐ŸŽฏ Final Portfolio ReportProfessor 12 SubjectsProfessor 24 Subjects๐Ÿ’ก Key Insight: Teaching Diversity vs Teaching LoadWe measure how many different subjects a professor CAN teach (versatility)NOT how many classes they teach (workload). Same subject in multiple depts = 1 unique skill.
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.
Asked in
Meta 15 Google 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~8 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