MySQL データベースの DQL 演習

目次

①、タイトル

準備作業(ワンクリックでコピー)

トピック 

答え

②、タイトル

準備作業(ワンクリックでコピー)

トピック 

答え

③、タイトル

準備作業(ワンクリックでコピー)

トピック

答え


以下は、私が集めたいくつかの厳選された質問 (回答を含む) です。役に立ったと思われる場合は、ワンクリックでトリプル リンクをお願いできます(いいえ)。

①、タイトル

準備作業(ワンクリックでコピー)

挿入する必要がある SQL データ: 1. 従業員テーブル、2. 部門テーブル、3. 給与等級テーブル。

/*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;

トピック 

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

答え

1) 従業員の名前、年齢、役職、部門情報を照会します。

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

検索結果: 


2) 30 歳未満の従業員の名前、年齢、役職、部署情報を照会します。

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;

検索結果: 

 
3) 従業員の部門 ID と部門名を照会します。

-- 写法一(内连接)
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);

検索結果: 

 
4) 40 歳以上のすべての従業員とその従業員が所属する部門の名前を照会します。従業員に部門が割り当てられていない場合は、それも表示する必要があります。

-- 写法一(左外连接)
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;

検索結果: 

5). すべての従業員の給与等級を照会します。

-- 写法一(内连接)
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;

検索結果: 


6). 「研究開発部門」の全従業員の情報と給与レベルを照会します。

-- 写法一(内连接):把三个表连接在一起进行查询
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;

検索結果: 

 
7). 「研究開発部門」の従業員の平均給与を照会します。

-- 写法一(内连接)
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 = '研发部');

検索結果: 

 
8) 給与が「消滅」よりも高い従業員の情報を照会します。

-- 写法一(子查询)
-- 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;

検索結果:  

9) 平均給与よりも高い従業員に関する情報を照会します。 

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

検索結果:   

10). 各部門の平均給与よりも高い給与を持つ従業員に関する情報を照会します。

-- 写法一(子查询)
-- 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);

検索結果:

-- 写法二(子查询)
-- 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;

検索結果:   

11) 部門の平均給与よりも給与が低い従業員の情報を照会します。

-- 写法一(子查询)
-- 第一步:查询员工表某一个部门的平均薪资
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);

検索結果:    

-- 写法二(子查询)
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;

検索結果:    

12) すべての部門情報を照会し、部門内の従業員の数を数えます。

-- 左外连接
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;

検索結果:    

②、タイトル

準備作業(ワンクリックでコピー)

挿入する必要がある SQL データ: 1. 学生テーブル、2. コース スケジュール、3. 学生コース中間テーブル。

/*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;

トピック 

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

答え

#1). 全学生の科目選択状況を問い合わせ、学生名、学生番号、科目名を表示します。

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;

③、タイトル

準備作業(ワンクリックでコピー)

挿入する必要がある SQL データ: 1. Student テーブル、2. Teacher テーブル、3. コース スケジュール、4. Grade テーブル。

/*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;

トピック

#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
#要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列;

答え

#01) 「01」コースのスコアが「02」コースのスコアよりも高い学生の情報とコーススコアを照会します (強調:「01」コースと「02」コースの両方を学習した学生)。

-- 第一步:查询" 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) 「01」コースと「02」コースが同時に存在する状況を問い合わせます。

-- 第一步:查询" 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) 「01」コースは存在するが「02」コースが存在しない可能性がある状況を問い合わせます(存在しない場合はnullと表示されます)。

-- 重点:" 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) 「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
right outer join
(
select * from t_score where cid = 2
) as b on a.sid = b.sid;


#05) 平均点が 60 点以上の生徒の生徒番号、生徒名​​、平均点をクエリします。

-- 第一步:查询平均成绩大于等于 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) t_score テーブルのスコアを使用して学生情報をクエリします。

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


#07) すべての学生の学生番号、学生名、受講したコースの合計数、およびすべてのコースの成績の合計をクエリします (成績のないものは 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) 姓が「Li」の教師の数をクエリします。

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


#09) 「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) すべてのコースを完了していない学生の情報を照会します。

-- 第一步:查询所有学生所学课程的数量<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) 「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) 2 つ以上のコースに落ちた学生の学生 ID 番号、名前、平均成績を照会します。

-- 第一步:查询两门及其以上不及格课程的同学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) 「01」コースのスコアが 60 未満の学生情報をスコアの降順で取得します。

-- 第一步:检索" 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) すべてのコースの全学生の成績と平均成績を高から低まで表示します。

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) 各科目の最高点、最低点、平均点を問い合わせます。 #
コースID、コース名、最高点、最低点、平均点、合格率、平均点、優秀率、優秀率、合格の形式で表示されます。 rate is >=60、medium is: 70-80、excellent is: 80-90、傑出した is: >=90
#コース番号と選択学生の数を出力する必要があります。クエリ結果は、 ; 人数が同じ場合はコース番号の昇順に並び替えます。

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;

以上。

おすすめ

転載: blog.csdn.net/weixin_62332711/article/details/128710496