2356. Number of Unique Subjects Taught by Each Teacher


Posted by ikl258794613 on 2024-02-26

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.

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

Return the result table in any order.

The result format is shown in the following example.

Example 1:

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

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.
SELECT teacher_id , COUNT(DISTINCT subject_id) AS cnt
FROM Teacher
GROUP BY teacher_id

#SQL







Related Posts

JS 變數宣告

JS 變數宣告

 建立屬於你的 Google Map 地圖標記(四) - 建立可拖曳的地圖標記

建立屬於你的 Google Map 地圖標記(四) - 建立可拖曳的地圖標記

筆記、[ALG101] 看程式

筆記、[ALG101] 看程式


Comments