Study - Problems(IT)/LeetCode - SQL

2356.Number of Unique Subjects Taught by Each Teacher

Dev.D 2024. 11. 20. 07:56

*Problem 

Write a solution to calculate the number of unique subjects each teacher teaches in the university.

Return the result table in any order.

 

*Explanation

Teacher 1:
  - They teach subject 2 in departments 3 and 4.
  - They teach subject 3 in department 3.
Teacher 2:
  - They teach subject 1 in department 1.
  - They teach subject 2 in department 1.
  - They teach subject 3 in department 1.
  - They teach subject 4 in department 1.

 

*Solution(Success)

# Write your MySQL query statement below
select teacher_id, 
       count(distinct(subject_id)) cnt
  from Teacher
  group by teacher_id