Number of Unique Subjects Taught by Each Teacher

Approach

select teacher_id, count(distinct(subject_id)) as cnt
from Teacher
group by teacher_id

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   |
+------------+-----+

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

- টিচার ডুপ্লিকেট হতে পারবে না, তাই গ্রুপ বাই টিচার করতে হবে 

- ইউনিক সাবজেক্ট কাউন্ট লাগবে, তাই সাবজেক্ট কাউন্ট করার আগে ডিস্টিংক্ট করে নিতে হবে 


select teacher_id, count(distinct(subject_id)) as cnt 
from Teacher 
group by teacher_id


Code Solution

Related video solution