当前位置:网站首页>每日sql-统计各个专业人数(包括专业人数为0的)

每日sql-统计各个专业人数(包括专业人数为0的)

2022-08-11 05:36:00 吃再多糖也不长胖

每日sql-统计各个专业人数(包括专业人数为0的)

DDL

CREATE TABLE IF NOT EXISTS student (student_id INT,student_name VARCHAR(45), gender VARCHAR(6), dept_id INT);CREATE TABLE IF NOT EXISTS department (dept_id INT, dept_name VARCHAR(255));insert into student (student_id, student_name, gender, dept_id) values (1, 'Jack', 'M', 1);insert into student (student_id, student_name, gender, dept_id) values (2, 'Jane', 'F', 1);insert into student (student_id, student_name, gender, dept_id) values (3, 'Mark', 'M', 2);insert into department (dept_id, dept_name) values (1, 'Engineering');insert into department (dept_id, dept_name) values (2, 'Science');insert into department (dept_id, dept_name) values (3, 'Law');

在这里插入图片描述

sql

select t2.dept_name,count(t1.student_id) as student_number from student t1 right join department t2 on t1.dept_id = t2.dept_id group by t2.dept_name;

在这里插入图片描述

原网站

版权声明
本文为[吃再多糖也不长胖]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43859562/article/details/122714481