学生表stu
字段名 | 数据类型 | 约束条件 |
s_id | varchar(10) | primary key |
s_name | varchar(10) | not null |
s_birth | date | |
s_sex | varchar(10) |
课程表co
字段名 | 数据类型 | 约束条件 |
c_id | varchar(10) | primary key |
c_name | varchar(10) | |
t_id | varchar(10) |
成绩表sc
字段名 | 数据类型 | 约束条件 |
s_id | varchar(10) | |
c_id | varchar(10) | |
score | int |
教师表te
字段名 | 数据类型 | 约束条件 |
t_id | varchar(10) |
primary key |
t_name | varchar(10) |
-- 创建数据库school
create database school;-- 选择进入school数据库
use school; -- ------------建表导数-------------
-- 创建stu
create table stu(
s_id varchar(10) primary key,
s_name varchar(10) not null,
s_birth date,
s_sex varchar(10));-- 导入数据
insert into stu values
('01' , '赵雷' , '1990-01-01' , '男'),
('02' , '钱电' , '1990-12-21' , '男'),
('03' , '孙风' , '1990-05-20' , '男'),
('04' , '李云' , '1990-08-06' , '男'),
('05' , '周梅' , '1991-12-01' , '女'),
('06' , '吴兰' , '1992-03-01' , '女'),
('07' , '郑竹' , '1992-04-21' , '女'),
('08' , '王菊' , '1990-01-20' , '女');select * from stu; -- 检查数据
select count(*) from stu; -- 检查总行数8 -- 创建co
create table co(
c_id varchar(10) primary key,
c_name varchar(10),
t_id varchar(10));-- 导入数据
insert into co values
('01' , '语文' , '02'),
('02' , '数学' , '01'),
('03' , '英语' , '03');select * from co; -- 检查数据
select count(*) from co; -- 检查总行数3 -- 创建te
create table te(
t_id varchar(10) primary key,
t_name varchar(10));-- 导入数据
insert into te values
('01' , '张三'),
('02' , '李四'),
('03' , '王五');select * from te; -- 检查数据
select count(*) from te; -- 检查总行数3 -- 创建sc
create table sc(
s_id varchar(10),
c_id varchar(10),
score int);-- 导入数据
insert into sc 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 sc; -- 检查数据
select count(*) from sc; -- 检查总行数18 -- ----------------------------------------------------------------------------------
-- 、查询"01"课程比"02"课程成绩高的学生信息及课程分数(选修的每一门课程的分数)
select stu.*,sc.c_id,score
from stu join sc on stu.s_id=sc.s_id
where stu.s_id in(select sc1.s_id
from sc sc1 join sc sc2 on sc1.s_id=sc2.s_id
where sc1.c_id='01' and sc2.c_id='02' and sc1.score>sc2.score);-- ----------------------------------------------------------------------------------
-- 、练习:查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select stu.*,sc.c_id,score
from stu join sc on stu.s_id=sc.s_id
where stu.s_id in(select sc1.s_id
from sc sc1 join sc sc2 on sc1.s_id=sc2.s_id
where sc1.c_id='01' and sc2.c_id='02' and sc1.score<sc2.score);-- ----------------------------------------------------------------------------------
-- 、查询学过"张三"老师授课的同学的信息
select stu.*
from stu ,sc,co,te
where stu.s_id=sc.s_id and sc.c_id=co.c_id and co.t_id=te.t_id and t_name='张三'; -- ----------------------------------------------------------------------------------
-- 、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select stu.*,score
from stu ,sc,co,te
where stu.s_id=sc.s_id and sc.c_id=co.c_id and co.t_id=te.t_id and t_name='张三'
order by score desc limit 1; -- ----------------------------------------------------------------------------------
-- 、练习:查询没学过"张三"老师授课的同学的信息
select *from stu
where s_id not in(select stu.s_id
from stu ,sc,co,te
where stu.s_id=sc.s_id and sc.c_id=co.c_id and co.t_id=te.t_id and t_name='张三'); -- ----------------------------------------------------------------------------------
-- 、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select *from stu
where s_id in(select sc1.s_id
from sc sc1 join sc sc2 on sc1.s_id=sc2.s_id
where sc1.c_id='01' and sc2.c_id='02' ); -- ----------------------------------------------------------------------------------
-- 、练习:查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from stu
where s_id in(select s_id from sc where c_id='01')
and s_id not in(select s_id from sc where c_id='02');-- ----------------------------------------------------------------------------------
-- 、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select stu.* from stu join sc on stu.s_id=sc.s_id
where c_id in(select c_id from sc where s_id='01')
and stu.s_id!='01' group by stu.s_id; -- ----------------------------------------------------------------------------------
-- 、练习:查询和"01"号的同学学习的课程完全相同的其他同学的信息
select stu.* from stu join sc on stu.s_id=sc.s_id
where sc.c_id in(select c_id from sc where s_id='01')
group by stu.s_id
having count(sc.c_id)=(select count(*) from sc where s_id='01') and stu.s_id!='01'; -- ----------------------------------------------------------------------------------
-- 、查询所有学生的课程及分数情况(一维转二维)
select stu.*
,sum(case when sc.c_id='01' then sc.score else null end) as '01'
,sum(case when sc.c_id='02' then sc.score else null end) as '02'
,sum(case when sc.c_id='03' then sc.score else null end) as '03'
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id;-- ----------------------------------------------------------------------------------
-- 、练习:按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select stu.*
,sum(case when sc.c_id='01' then sc.score else null end) as '01'
,sum(case when sc.c_id='02' then sc.score else null end) as '02'
,sum(case when sc.c_id='03' then sc.score else null end) as '03'
,avg(score)
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id order by avg(score) desc; -- ----------------------------------------------------------------------------------
-- 、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
select co.c_id 课程ID,c_name 课程name ,max(score) 最高分,
min(score) 最低分,avg(score) 平均分,
concat(round(sum(case when score>=60 then 1 else 0 end)/count(*)*100),'%') 及格率,
concat(round(sum(case when score>=70 and score<80 then 1 else 0 end)/count(*)*100),'%') 中等率,
concat(round(sum(case when score>=80 and score<90 then 1 else 0 end)/count(*)*100),'%') 优良率,
concat(round(sum(case when score>=90 then 1 else 0 end)/count(*)*100),'%') 优秀率
from co join sc on co.c_id=sc.c_id
group by co.c_id;-- ----------------------------------------------------------------------------------
-- 、练习:统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select co.c_id,c_name,
sum(case when score>=85 then 1 else 0 end) as '[100-85]',
sum(case when score>=70 and score<85 then 1 else 0 end) as '[85-70]',
sum(case when score>=60 and score<70 then 1 else 0 end) as '[70-60]',
sum(case when score>=85 then 1 else 0 end) as '[0-60]'
from co join sc on co.c_id=sc.c_id
group by co.c_id; select co.c_id,c_name,
concat(round(sum(case when score>=85 then 1 else 0 end)/count(*)*100),'%') as '[100-85]%',
concat(round(sum(case when score>=70 and score<85 then 1 else 0 end)/count(*)*100),'%') as '[85-70]%',
concat(round(sum(case when score>=60 and score<70 then 1 else 0 end)/count(*)*100),'%') as '[70-60]%',
concat(round(sum(case when score<60 then 1 else 0 end)/count(*)*100),'%') as '[0-60]%'
from co join sc on co.c_id=sc.c_id
group by co.c_id; select a.c_id,a.c_name,a.优良,b.优良比例,a.良好,b.良好比例,a.中等,b.中等比例,a.不及格,b.不及格比例
from (select co.c_id,c_name,
sum(case when score>=85 then 1 else 0 end) as 优良,
sum(case when score>=70 and score<85 then 1 else 0 end) as 良好,
sum(case when score>=60 and score<70 then 1 else 0 end) as 中等,
sum(case when score>=85 then 1 else 0 end) as 不及格
from co join sc on co.c_id=sc.c_id
group by co.c_id) a
join (select co.c_id,c_name,
concat(round(sum(case when score>=85 then 1 else 0 end)/count(*)*100),'%') as 优良比例,
concat(round(sum(case when score>=70 and score<85 then 1 else 0 end)/count(*)*100),'%') as 良好比例,
concat(round(sum(case when score>=60 and score<70 then 1 else 0 end)/count(*)*100),'%') as 中等比例,
concat(round(sum(case when score<60 then 1 else 0 end)/count(*)*100),'%') as 不及格比例
from co join sc on co.c_id=sc.c_id
group by co.c_id) b
on a.c_id=b.c_id; -- ----------------------------------------------------------------------------------
-- 、查询学生的总成绩并进行排名
select stu.s_id,s_name,sum(score),row_number () over( order by sum(score) desc )
from stu , sc
where stu.s_id=sc.s_id
group by stu.s_id ;
-- ----------------------------------------------------------------------------------
-- 、练习:查询每个学生平均成绩及其名次
select stu.s_id,s_name,avg(score),row_number () over( order by avg(score) desc )
from stu , sc
where stu.s_id=sc.s_id
group by stu.s_id ;
-- ----------------------------------------------------------------------------------
-- 、按各科成绩进行排序,并显示排名
select c_id,score,row_number () over(partition by c_id order by score desc ) as pm
from sc
;
-- ----------------------------------------------------------------------------------
-- 、查询各科成绩前三名的记录
select c_id,score,pm from
(select c_id,score,row_number () over(partition by c_id order by score desc )as pm
from sc ) as a
where a.pm<4; -- ----------------------------------------------------------------------------------
-- 、练习:查询每门功成绩最好的前两名
select c_id,score,pm from
(select c_id,score,row_number () over(partition by c_id order by score desc )as pm
from sc ) as a
where a.pm<3; -- ----------------------------------------------------------------------------------
-- 、练习:查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select stu.*,a.c_id,a.score,a.pm from
(select s_id,c_id,score,row_number () over(partition by c_id order by score desc )as pm
from sc ) as a join stu on a.s_id=stu.s_id
where a.pm in(2,3);-- ----------------------------------------------------------------------------------
-- 、查询同名同姓学生名单,并统计同名人数
select s_name,if(count(s_name)=1,0,count(s_name))
from stu
group by s_name; -- ----------------------------------------------------------------------------------
-- 、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select s_name,c_name,score
from stu join sc on stu.s_id=sc.s_id
join co on sc.c_id=co.c_id
where score>=70; -- ----------------------------------------------------------------------------------
-- 、练习:查询出现过学生考试不及格的课程
select distinct co.c_id,c_name from sc join co on sc.c_id=co.c_id
where score<60; -- ----------------------------------------------------------------------------------
-- 、查询课程不同、成绩相同的学生的学生编号、课程编号、学生成绩
select a.s_id,a.c_id,a.score from sc a join sc b on a.c_id!=b.c_id and a.score=b.score; -- ----------------------------------------------------------------------------------
-- 、查询本周过生日的学生
select * from stu
where week(s_birth)=week(curdate()); -- ----------------------------------------------------------------------------------
-- 、练习:查询下周过生日的学生
select * from stu
where week(s_birth)=week(curdate())+1;
-- ----------------------------------------------------------------------------------
-- 、查询本月过生日的学生
select s_name,month(s_birth) from stu
where month(s_birth)=month(curdate()); select * from stu
where MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))=month(s_birth);-- ----------------------------------------------------------------------------------
-- 、练习:查询下月过生日的学生
select s_name,month(s_birth) from stu
where month(s_birth)-1=month(curdate());