LeetCode 2356.每位教师所教授的科目种类的数量

数据准备

Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int);
Truncate table Teacher;
insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '3');
insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '4');
insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '3', '3');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '1', '1');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '2', '1');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '3', '1');
insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '4', '1');

需求

写一个 SQL 来查询每位老师在大学里教授的科目种类的数量。

输入

LeetCode 2356.每位教师所教授的科目种类的数量_第1张图片

输出

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

LeetCode 2356.每位教师所教授的科目种类的数量_第2张图片

你可能感兴趣的:(数据库,leetcode,mysql,算法)