SQL-leetcode— 2356. 每位教师所教授的科目种类的数量

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

表: Teacher

±------------±-----+

| Column Name | Type |

±------------±-----+

| teacher_id | int |

| subject_id | int |

| dept_id | int |

±------------±-----+

在 SQL 中,(subject_id, dept_id) 是该表的主键。

该表中的每一行都表示带有 teacher_id 的教师在系 dept_id 中教授科目 subject_id。

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

以 任意顺序 返回结果表。

查询结果格式示例如下。

示例 1:

输入:

Teacher 表:

±-----------±-----------±--------+

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

±-----------±-----------±--------+

输出:

±-----------±----+

| teacher_id | cnt |

±-----------±----+

| 1 | 2 |

| 2 | 4 |

±-----------±----+

解释:

教师 1:

  • 他在 3、4 系教科目 2。
  • 他在 3 系教科目 3。
    教师 2:
  • 他在 1 系教科目 1。
  • 他在 1 系教科目 2。
  • 他在 1 系教科目 3。
  • 他在 1 系教科目 4。

题解

方法一

复制代码
select
    teacher_id,count(distinct subject_id) as cnt
from Teacher group by teacher_id
相关推荐
素玥1 天前
实训5 python连接mysql数据库
数据库·python·mysql
jnrjian1 天前
text index 查看index column index定义 index 刷新频率 index视图
数据库·oracle
瀚高PG实验室1 天前
审计策略修改
网络·数据库·瀚高数据库
言慢行善1 天前
sqlserver模糊查询问题
java·数据库·sqlserver
韶博雅1 天前
emcc24ai
开发语言·数据库·python
有想法的py工程师1 天前
PostgreSQL 分区表排序优化:Append Sort 优化为 Merge Append
大数据·数据库·postgresql
迷枫7121 天前
达梦数据库的体系架构
数据库·oracle·架构
夜晚打字声1 天前
9(九)Jmeter如何连接数据库
数据库·jmeter·oracle
Chasing__Dreams1 天前
Mysql--基础知识点--95--为什么避免使用长事务
数据库·mysql
NineData1 天前
NineData 智能数据管理平台新功能发布|2026 年 3 月
数据库·oracle·架构·dba·ninedata·数据复制·数据迁移工具