DQL exercises for MySQL database

Table of contents

①、Title

Preparation work (copy with one click)

topic 

Answer

②、Title

Preparation work (copy with one click)

topic 

Answer

③、Title

Preparation work (copy with one click)

topic

Answer


Below are some select questions (including answers) that I collected. If you find them useful, can you give me a one-click triple link (no)! ! !

①、Title

Preparation work (copy with one click)

SQL data that needs to be inserted: 1. Employee table; 2. Department table; 3. Salary grade table.

/*1、员工表*/
create table emp(
id int auto_increment primary key comment '员工ID',
name varchar(50) not null comment '姓名',
age int comment '年龄',
job varchar(20) comment '职位',
salary int comment '薪资',
entrydate date comment '入职时间',
managerid int comment '直属领导ID',
dept_id int comment '部门ID'
)comment '员工表';

/*给员工表插入数据*/
insert into emp (id, name, age, job,salary, entrydate, managerid, dept_id)
values
(1, '金庸', 66, '总裁',20000, '2000-01-01', null,5),
(2, '张无忌', 20, '项目经理',12500, '2005-12-05', 1,1),
(3, '杨逍', 33, '开发', 8400,'2000-11-03', 2,1),
(4, '韦一笑', 48, '开发',11000, '2002-02-05', 2,1),
(5, '常遇春', 43, '开发',10500, '2004-09-07', 3,1),
(6, '小昭', 19, '程序员鼓励师',6600, '2004-10-12', 2,1),
(7, '灭绝', 60, '财务总监',8500, '2002-09-12', 1,3),
(8, '周芷若', 19, '会计',48000, '2006-06-02', 7,3),
(9, '丁敏君', 23, '出纳',5250, '2009-05-13', 7,3),
(10, '赵敏', 20, '市场部总监',12500, '2004-10-12', 1,2),
(11, '鹿杖客', 56, '职员',3750, '2006-10-03', 10,2),
(12, '鹤笔翁', 19, '职员',3750, '2007-05-09', 10,2),
(13, '方东白', 19, '职员',5500, '2009-02-12', 10,2),
(14, '张三丰', 88, '销售总监',14000, '2004-10-12', 1,4),
(15, '俞莲舟', 38, '销售',4600, '2004-10-12', 14,4),
(16, '宋远桥', 40, '销售',4600, '2004-10-12', 14,4),
(17, '陈友谅', 42, null,2000, '2011-10-12', 1,null);

/*2、部门表*/
create table dept(
id int auto_increment primary key comment '部门ID',
name varchar(50) not null comment '部门名称'
)comment '部门表';

/*给部门表插入数据*/
insert into dept (id, name) 
VALUES 
(1, '研发部'),
(2, '市场部'),
(3, '财务部'),
(4, '销售部'),
(5, '总经办'),
(6, '人事部');

/*给员工表和部门表添加外键关系*/
alter table emp add constraint fk_emp_dept_id foreign key(dept_id) references dept(id);


/*3、薪资等级表*/
create table salgrade(
grade int comment '薪资等级',
losal int comment '最低薪资',
hisal int comment '最高薪资'
) comment '薪资等级表';


/*给薪资等级表插入数据*/
insert into salgrade values (1,0,3000),
(2,3001,5000),
(3,5001,8000),
(4,8001,10000),
(5,10001,15000),
(6,15001,20000),
(7,20001,25000),
(8,25001,30000);
#查询员工表
select * from emp;
#查询部门表
select * from dept;
#查询薪资等级表
select * from salgrade;

topic 

#1)、查询员工的姓名、年龄、职位、部门信息;
#2)、查询年龄小于30岁的员工的姓名、年龄、职位、部门信息;
#3)、查询拥有员工的部门id、部门名称;
#4)、查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出来;
#5)、查询所有员工的工资等级;
#6)、查询 "研发部" 所有员工的信息及工资等级;
#7)、查询 "研发部" 员工的平均工资;
#8)、查询工资比 "灭绝" 高的员工信息;
#9)、查询比平均薪资高的员工信息;
#10)、查询比每个部门平均薪资高的员工信息;
#11)、查询低于本部门平均工资的员工信息;
#12)、查询所有的部门信息, 并统计部门的员工人数。

Answer

1) Query the employee’s name, age, position, and department information;

select
e.name,e.age,e.job,d.*
from
emp as e,dept as d
where e.dept_id = d.id;

search result: 


2) Query the name, age, position, and department information of employees younger than 30 years old;

select
e.name,e.age,e.job,d.*
from
emp as e,dept as d
where e.dept_id = d.id
and e.age < 30;

search result: 

 
3) Query the department ID and department name of employees;

-- 写法一(内连接)
select distinct
d.*
from
emp as e,dept as d
where e.dept_id = d.id;

-- 写法二(子查询)
-- 1、先查出员工表里面有部门的员工部门id(dept_id)
select dept_id from emp;
-- 2、再查出在部门表里面部门id中有没有包含这些员工的部门id(dept_id)
select * from dept where id in (select dept_id from emp);

search result: 

 
4) Query all employees over 40 years old and the name of the department to which they belong; if the employee is not assigned a department, it also needs to be displayed;

-- 写法一(左外连接)
select
*
from
emp as e left outer join dept as d
on e.dept_id = d.id
where e.age > 40;

-- 写法二(子查询)
-- 1、查询员工表所有年龄大于40岁的员工
select * from emp where age > 40;
-- 2、再把员工表和部门表连接在一起,查询所有年龄大于40岁的员工, 及其归属的部门名称
select * from (select * from emp where age > 40) as e,dept as d where e.dept_id = d.id;
-- 3、使用左外连接,查询出所有emp表的所有员工,也就可以查询出没有分配部门的员工
select * from (select * from emp where age > 40) as e left outer join dept as d on e.dept_id = d.id;

search result: 

5). Query the salary grades of all employees;

-- 写法一(内连接)
select
e.*,s.grade
from emp as e,salgrade as s
where e.salary between s.losal and s.hisal;

-- 或者把“e.salary between s.losal and s.hisal”改成“e.salary >= s.losal and e.salary <= s.hisal”也行
select
e.*,s.grade
from emp as e,salgrade as s
where e.salary >= s.losal and e.salary <= s.hisal;

-- 写法二(子查询)
-- 1、先查询出工资等级表的内容
select * from salgrade;
-- 2、再把工资等级表和员工表连接在一起,查询所有员工的工资等级
select e.*,s.grade from emp as e,(select * from salgrade) as s where e.salary between s.losal and s.hisal;

-- 或者把“e.salary between s.losal and s.hisal”改成“e.salary >= s.losal and e.salary <= s.hisal”也行
select e.*,s.grade from emp as e,(select * from salgrade) as s where e.salary >= s.losal and e.salary <= s.hisal;

search result: 


6). Query the information and salary levels of all employees in the "R&D Department";

-- 写法一(内连接):把三个表连接在一起进行查询
select * from
emp as e,dept as d,salgrade as s
where e.dept_id = d.id
and d.name = '研发部'
and e.salary between s.losal and s.hisal;

-- 写法二(子查询)
-- 1、查询部门表中 "研发部" 的部门id
select id from dept where name = '研发部';
-- 2、查询 "研发部" 所有员工的信息
select * from emp where dept_id = (select id from dept where name = '研发部');
-- 3、把"研发部" 所有员工的信息和工资等级表连接在一起,查出 "研发部" 所有员工的信息及工资等级
select * from (select * from emp where dept_id = (select id from dept where name = '研发部')) as a,salgrade as s where a.salary >= s.losal and a.salary <= s.hisal;

search result: 

 
7). Query the average salary of employees in the "R&D Department";

-- 写法一(内连接)
select avg(e.salary) from
emp as e,dept as d
where e.dept_id = d.id
and d.name = '研发部';

-- 写法二(子查询)
-- 1、查询部门表中 "研发部" 的部门id
select id from dept where name = '研发部';
-- 2、再把部门表和员工表连接在一起,查询 "研发部" 员工的平均工资
select avg(emp.salary) from emp where emp.dept_id = (select id from dept where name = '研发部');

search result: 

 
8) Query the information of employees whose salary is higher than "extinction";

-- 写法一(子查询)
-- 1、查询出员工表中 "灭绝" 的工资
select salary from emp where name = '灭绝';
-- 2、查询工资比 "灭绝" 高的员工信息
select * from emp where salary > (select salary from emp where name = '灭绝');

-- 写法二(自连接)
-- 1、先自己连接自己
select * from emp as e1,emp as e2;
-- 2、查询出e1表中 "灭绝" 的工资
select * from emp as e1,emp as e2 where e1.name = '灭绝';
-- 3、查询出e2表里面比e1表中 "灭绝" 的工资高的员工信息
select e2.* from emp as e1,emp as e2 where e1.name = '灭绝' and e2.salary > e1.salary;

search result:  

9) Query information about employees with higher than average salary; 

-- 子查询
-- 1、查询员工表的平均薪资
select avg(salary) from emp;
-- 2、查询比平均薪资高的员工信息
select * from emp
where salary > (select avg(salary) from emp);

search result:   

10). Query information about employees with higher salary than the average salary of each department;

-- 写法一(子查询)
-- 1、查询员工表某一个部门的平均薪资
select avg(salary) from emp where dept_id = 1;
-- 2、查询比每个部门平均薪资高的员工信息
select * from emp as e1 where salary > all (select avg(salary) from emp as e2 where e1.dept_id = e2.dept_id);

search result:

-- 写法二(子查询)
-- 1、员工表和部门表进行联表,查询出每个部门的平均工资
select e.dept_id,d.name,avg(e.salary) from emp as e,dept as d where e.dept_id = d.id group by e.dept_id;

-- 2、查询比平均薪资高的员工信息
select * from
emp as e1,
(select e.dept_id,d.name,avg(e.salary) avg from emp as e,dept as d where e.dept_id = d.id group by e.dept_id) as e2
where e1.dept_id = e2.dept_id
and e1.salary > e2.avg;

-- 3、过滤出需要的信息
select e1.*,e2.name,avg from
emp as e1,
(select e.dept_id,d.name,avg(e.salary) avg from emp as e,dept as d where e.dept_id = d.id group by e.dept_id) as e2
where e1.dept_id = e2.dept_id
and e1.salary > e2.avg;

search result:   

11) Query the information of employees whose salary is lower than the average salary of the department;

-- 写法一(子查询)
-- 第一步:查询员工表某一个部门的平均薪资
select avg(salary) from emp where dept_id = 1;
-- 第二步:查询低于本部门平均工资的员工信息
select * from
emp as e2 
where salary < (select avg(salary) from emp as e1 where e1.dept_id = e2.dept_id);

search result:    

-- 写法二(子查询)
select e1.*,e2.name,avg from
emp as e1,
(select e.dept_id,d.name,avg(e.salary) avg from emp as e,dept as d where e.dept_id = d.id group by e.dept_id) as e2
where e1.dept_id = e2.dept_id
and e1.salary < e2.avg;

search result:    

12) Query all department information and count the number of employees in the department;

-- 左外连接
select d.*,count(e.dept_id) from dept as d left outer join emp as e on d.id = e.dept_id group by e.dept_id;

-- 子查询
-- 第一步:查询员工表某一个部门的员工人数
select count(*) from emp where dept_id = 6;
-- 第二步:查询所有的部门信息, 并统计部门的员工人数
-- form先执行,然后再执行select
select d.*,(select count(*) from emp where dept_id = d.id) from dept as d;

search result:    

②、Title

Preparation work (copy with one click)

SQL data that needs to be inserted: 1. Student table; 2. Course schedule; 3. Student course intermediate table.

/*1、学生表*/
create table student(
id int auto_increment primary key comment '主键ID',
name varchar(10) comment '姓名',
no varchar(10) comment '学号'
) comment '学生表';

/*给学生表插入数据*/
insert into student
values
(null, '黛绮丝', '2000100101'),
(null, '谢逊', '2000100102'),
(null, '殷天正', '2000100103'),
(null, '韦一笑', '2000100104');

/*2、课程表*/
create table course(
id int auto_increment primary key comment '主键ID',
name varchar(10) comment '课程名称'
 ) comment '课程表';

/*给课程表插入数据*/
insert into course
values
(null, 'Java'),
(null, 'PHP'),
(null , 'MySQL'),
(null, 'Hadoop');

/*3、学生课程中间表*/
create table student_course(
id int auto_increment comment '主键ID' primary key,
studentid int not null comment '学生ID',
courseid int not null comment '课程ID',
-- 跟课程表和学生表建立外键关系
constraint fk_courseid foreign key (courseid) references course (id),
constraint fk_studentid foreign key (studentid) references student (id)
)comment '学生课程中间表';

/*给学生课程中间表插入数据*/
insert into student_course
values
(null,1,1),
(null,1,2),
(null,1,3),
(null,2,2),
(null,2,3),
(null,3,4);
select * from student;
select * from course;
select * from student_course;

topic 

#1). 查询所有学生的选课情况, 展示出学生名称, 学号, 课程名称

Answer

#1). Query the course selection status of all students and display the student name, student number, and course name

select st.name,st.no,co.name 
from
student as st,
course as co,
student_course as sc
where st.id = sc.studentid and co.id = sc.courseid;

③、Title

Preparation work (copy with one click)

SQL data that needs to be inserted: 1. Student table; 2. Teacher table; 3. Course schedule; 4. Grade table.

/*1、学生表*/
create table t_student(
    sid int primary key auto_increment, -- 学生编号
    sname varchar(20) not null, -- 学生姓名
    sage date not null, -- 学生年龄
    ssex varchar(10) not null -- 学生性别
);

/*给学生表插入数据*/
insert into t_student 
values
('01' , '赵雷' , '1990-01-01' , '男'),
('02' , '钱电' , '1990-12-21' , '男'),
('03' , '孙风' , '1990-12-20' , '男'),
('04' , '李云' , '1990-12-06' , '男'),
('05' , '周梅' , '1991-12-01' , '女'),
('06' , '吴兰' , '1992-01-01' , '女'),
('07' , '郑竹' , '1989-01-01' , '女'),
('09' , '张三' , '2017-12-20' , '女'),
('10' , '李四' , '2017-12-25' , '女'),
('11' , '李四' , '2012-06-06' , '女'),
('12' , '赵六' , '2013-06-13' , '女'),
('13' , '孙七' , '2014-06-01' , '女');

/*2、教师表*/
create table t_teacher(
    tid int primary key auto_increment, -- 教师编号
    tname varchar(20) not null -- 教师名称
);

/*给教师表插入数据*/
insert into t_teacher
values
('01' , '张三'),
('02' , '李四'),
('03' , '王五');

/*3、课程表*/
create table t_course(
    cid int primary key auto_increment, -- 课程编号
    cname varchar(30) not null unique, -- 课程名称
    tid varchar(20) not null -- 教师名称
);

/*给课程表插入数据*/
insert into t_course 
values
('01' , '语文' , '02'),
('02' , '数学' , '01'),
('03' , '英语' , '03');

/*4、成绩表*/
create table t_score(
    sid int not null, -- 学生编号
    cid int not null, -- 课程编号
    score float not null -- 学生成绩
);

/*给成绩表插入数据*/
insert into t_score 
values
('01' , '01' , 80),
('01' , '02' , 90),
('01' , '03' , 99),
('02' , '01' , 70),
('02' , '02' , 60),
('02' , '03' , 80),
('03' , '01' , 80),
('03' , '02' , 80),
('03' , '03' , 80),
('04' , '01' , 50),
('04' , '02' , 30),
('04' , '03' , 20),
('05' , '01' , 76),
('05' , '02' , 87),
('06' , '01' , 31),
('06' , '03' , 34),
('07' , '02' , 89),
('07' , '03' , 98);
select * from t_student;
select * from t_teacher;
select * from t_course;
select * from t_score;

topic

#01)查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数(重点:要同时学了" 01 "课程和" 02 "课程的学生);
#02)查询同时存在" 01 "课程和" 02 "课程的情况;
#03)查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null );
#04)查询不存在" 01 "课程但存在" 02 "课程的情况;
#05)查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩;
#06)查询在t_score表存在成绩的学生信息;
#07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null );
#08)查询「李」姓老师的数量;
#09)查询学过「张三」老师授课的同学的信息;
#10)查询没有学全所有课程的同学的信息;
#11)查询没学过"张三"老师讲授的任一门课程的学生姓名;
#12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩;
#13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息;
#14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩;
#15)查询各科成绩最高分、最低分和平均分:
#以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
#要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列;

Answer

#01) Query the information and course scores of students whose scores in the "01" course are higher than those in the "02" course (emphasis: students who have studied both the "01" course and the "02" course);

-- 第一步:查询" 01 "课程和" 02 "课程的学生情况
select * from t_score where cid = 1;
select * from t_score where cid = 2;
-- 第二步:查询同时学了" 01 "课程和" 02 "课程的学生,并且" 01 "课程比" 02 "课程成绩高的课程分数
select a.sid,a.score as '01score' from
(select * from t_score where cid = 1) as a,
(select * from t_score where cid = 2) as b
where a.sid = b.sid and a.score > b.score;
-- 第三步:查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
select tst.*,c.01score from(

select a.sid,a.score as '01score' from
(select * from t_score where cid = 1) as a,
(select * from t_score where cid = 2) as b
where a.sid = b.sid and a.score > b.score

) as c,t_student as tst where c.sid = tst.sid;


#02) Query the situation where "01" course and "02" course exist at the same time;

-- 第一步:查询" 01 "课程和" 02 "课程的学生情况
select * from t_score where cid = 1;
select * from t_score where cid = 2;

-- 第二步:查询同时学了" 01 "课程和" 02 "课程的学生
select a.sid as '01sid',a.score as '01score',b.sid as '02sid',b.score '02score' from
(select * from t_score where cid = 1) as a,
(select * from t_score where cid = 2) as b
where a.sid = b.sid;

-- 第三步:查询同时学了" 01 "课程和" 02 "课程的学生信息
select * from t_student as tst,(

select a.sid as '01sid',a.score as '01score',b.sid as '02sid',b.score '02score' from
(select * from t_score where cid = 1) as a,
(select * from t_score where cid = 2) as b
where a.sid = b.sid

) as c where tst.sid = 01sid and tst.sid = 02sid;


#03) Query the situation where the "01" course exists but the "02" course may not exist (it will be displayed as null if it does not exist);

-- 重点:" 01 "课程和" 02 "课程都要存在,但是要查出存在" 01 "课程但可能不存在" 02 "课程的情况
-- 第一步:查询" 01 "课程和" 02 "课程的学生情况
select * from t_score where cid = 1;
select * from t_score where cid = 2;

-- 第二步:查询存在" 01 "课程但可能不存在" 02 "课程的情况
select * from
(
select * from t_score where cid = 1
) as a
left outer join
(
select * from t_score where cid = 2
) as b on a.sid = b.sid;


#04) Query the situation where "01" course does not exist but "02" course exists;

-- 第一步:查询" 01 "课程和" 02 "课程的学生情况
select * from t_score where cid = 1;
select * from t_score where cid = 2;
-- 第二步:查询不存在" 01 "课程但存在" 02 "课程的情况
select * from
(
select * from t_score where cid = 1
) as a
right outer join
(
select * from t_score where cid = 2
) as b on a.sid = b.sid;


#05) Query the student number, student name and average score of students whose average score is greater than or equal to 60 points;

-- 第一步:查询平均成绩大于等于 60 分的情况
select sid,avg(score) as avgs from t_score group by sid having avgs >= 60;
-- 第二步:查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩;
select tst.sid,tst.sname,avgs
from
t_student as tst,
(select sid,avg(score) as avgs from t_score group by sid having avgs >= 60) as a
where a.sid = tst.sid;


#06) Query student information with scores in the t_score table;

select distinct tst.* from t_score as tco,t_student as tst where tco.sid = tst.sid;


#07) Query the student number, student name, total number of courses taken, and total grades of all courses for all students (those without grades are displayed as null);

-- 第一步:查询所有同学的学生编号、选课总数、所有课程的总成绩
select sid,count(*) as '选课总数',sum(score) as '课程总成绩' from t_score group by sid;
-- 第二步:查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select tst.sid,tst.sname,a.选课总数,a.课程总成绩
from
t_student as tst
left outer join
(select sid,count(*) as '选课总数',sum(score) as '课程总成绩' from t_score group by sid) as a
on tst.sid = a.sid;


#08) Query the number of teachers with the surname "Li";

select count(*) from t_teacher where tname like'李%';


#09) Query the information of students who have studied with teacher "Zhang San";

-- 第一步:查询「张三」老师授的课程
select cid from t_teacher as tte,t_course as tco where tte.tid = tco.tid and tte.tname = '张三';
-- 第二步:查询学过「张三」老师授课的学生id
select sid from t_score where cid = (select cid from t_teacher as tte,t_course as tco where tte.tid = tco.tid and tte.tname = '张三');
-- 第三步:查询学过「张三」老师授课的同学的信息
select * from t_student where sid in
(
select sid from t_score where cid = (select cid from t_teacher as tte,t_course as tco where tte.tid = tco.tid and tte.tname = '张三')
);


#10) Query the information of students who have not completed all courses;

-- 第一步:查询所有学生所学课程的数量<3的学生id
select sid,count(*) counts from t_score group by sid having counts < 3;
-- 第二步:查询没有学全所有课程的同学的信息
select * from t_student as tst,
(
select sid,count(*) counts from t_score group by sid having counts < 3
) as a
where tst.sid = a.sid;


#11) Query the names of students who have not studied any course taught by "Zhang San";

select * from t_student where sid not in
(
select sid from t_score where cid = (select cid from t_teacher as tte,t_course as tco where tte.tid = tco.tid and tte.tname = '张三')
);


#12) Query the student ID number, name and average grade of students who have failed two or more courses;

-- 第一步:查询两门及其以上不及格课程的同学sid,数量,平均成绩
select sid,count(*) counts,avg(score) from t_score where score < 60 group by sid having counts >= 2;
-- 第二步:查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select tst.sid,tst.sname,a.avgs
from t_student as tst,
(
select sid,count(*) counts,avg(score) avgs from t_score where score < 60 group by sid having counts >= 2
) as a
where tst.sid = a.sid;


#13) Retrieve student information with "01" course scores less than 60, sorted by scores in descending order;

-- 第一步:检索" 01 "课程分数小于 60,按分数降序排列的学生sid
select sid from t_score where cid = 1 and score < 60 order by score desc;
-- 第二步:检索" 01 "课程分数小于 60,按分数降序排列的学生sid
select * from t_student where sid in (select sid from t_score where cid = 1 and score < 60 order by score desc);


#14) Display the grades of all students in all courses and the average grade from high to low;

select tst.*,tco.cname,avgs from t_student as tst,t_course as tco,t_score as tsc,(
select sid,avg(score) avgs from t_score group by sid
) as a where tst.sid = a.sid and tst.sid = tsc.sid and tco.cid = tsc.cid order by avgs desc;

#15) Query the highest, lowest and average scores of each subject:
#Displayed in the following form: course ID, course name, highest score, lowest score, average score, passing rate, average rate, excellent rate, excellent rate, passing rate is >=60, medium is: 70-80, excellent is: 80-90, excellent is: >=90
#Requires to output the course number and the number of elective students. The query results are arranged in descending order by the number of people. If the number of people is the same, they are arranged in ascending order by the course number. ;

select tco.cid as 课程ID,tco.cname as 课程name,max(tsc.score) as 最高分,min(tsc.score) as 最低分,avg(tsc.score) as 平均分,count(tco.cid) 课程人数,
sum(case when tsc.score >= 60 then 1 else 0 end)/count(tco.cid) as 及格率,
sum(case when tsc.score >= 70 and tsc.score >= 80 then 1 else 0 end)/count(tco.cid) as 中等率,
sum(case when tsc.score >= 80 and tsc.score >= 90 then 1 else 0 end)/count(tco.cid) as 优良率,
sum(case when tsc.score >= 90 then 1 else 0 end) * 100 as 优秀率
from
t_course as tco,t_score as tsc where tco.cid = tsc.cid group by tco.cid
order by count(tco.cid) desc,tco.cid asc;

over. . .

Guess you like

Origin blog.csdn.net/weixin_62332711/article/details/128710496