SQL50道练习题

– 创建数据库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; – 检查总行数

– 创建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; – 检查总行数

– 创建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; – 检查总行数

– 创建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; – 检查总行数

select * from co;
select * from sc;
select * from stu;
select * from te;


– 1、查询"01"课程比"02"课程成绩高的学生信息及课程分数
– 思路:01课程、02课程比较筛选,再与成绩表和学生信息表连接得到所需信息
select s_id,score from sc where c_id=‘01’;
select s_id,score from sc where c_id=‘02’;

select stu.*,sc.c_id,sc.score
from (select s_id,score from sc where c_id=‘01’) t1
join (select s_id,score from sc where c_id=‘02’) t2 on t1.s_id=t2.s_id
join stu on t1.s_id=stu.s_id
join sc on stu.s_id=sc.s_id
where t1.score>t2.score;


– 2、练习:查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select *
from (select s_id,score from sc where c_id=‘01’) t1
join (select s_id,score from sc where c_id=‘02’) t2 on t1.s_id=t2.s_id
join stu on t1.s_id=stu.s_id
join sc on stu.s_id=sc.s_id
where t1.score<t2.score;


– 3、查询平均成绩大于等于60分的同学的学生编号、学生姓名和平均成绩
– 思路:先按学号分组 得出平均成绩 再与学生表连接
select sc.s_id,stu.s_name,avg(score)
from sc
join stu on sc.s_id=stu.s_id
group by s_id
having avg(score)>=60;

select t.s_id,平均成绩,stu.s_name
from (select s_id,avg(score) 平均成绩 from sc group by s_id having avg(score)>=60) t
join stu on t.s_id=stu.s_id;

– 数据量相同的情况下,表连接效率会高于子查询


– 4、练习:查询平均成绩小于60分的同学的学生编号、学生姓名和平均成绩
select stu.s_id,s_name,平均成绩
from(select s_id,avg(score)平均成绩 from sc group by s_id having avg(score)<60) t
join stu on t.s_id=stu.s_id;


– 33、练习:查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select s_id,avg(score)
from sc
group by s_id – 分组字段
having avg(score)>=85; – 聚合字段

select stu.s_id,s_name,平均成绩
from(select s_id,avg(score)平均成绩 from sc group by s_id having avg(score)>=85) t
join stu on t.s_id=stu.s_id;


– 5、练习:查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
– 思路:所有学生 先连接学生表和成绩表,再按学号分组
select stu.s_id,s_name,count(c_id) 选课总数,sum(score) 总成绩
from stu
left join sc on stu.s_id=sc.s_id
group by stu.s_id;


– 6、查询"李"姓老师的数量
– 考点:模糊匹配 like的使用
select count(t_id) from te where t_name like ‘李%’;


– 29、练习:查询名字中含有"风"字的学生信息
select * from stu where s_name like ‘%风%’;


– 7、查询学过"张三"老师授课的同学的信息
select stu.* from te
left join co on te.t_id=co.t_id
left join sc on co.c_id=sc.c_id
left join stu on sc.s_id=stu.s_id
where te.t_name= ‘张三’;


– 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select stu.*,score
from te
left join co on te.t_id=co.t_id
left join sc on co.c_id=sc.c_id
left join stu on sc.s_id=stu.s_id
where te.t_name= ‘张三’
order by score desc
limit 1;

select p.,max(score)
from (select stu.
,score
from te
left join co on te.t_id=co.t_id
left join sc on co.c_id=sc.c_id
left join stu on sc.s_id=stu.s_id
where te.t_name= ‘张三’) p;


– 8、练习:查询没学过"张三"老师授课的同学的信息
– 先选出在此范围内,反向排除
– not in
select * from stu where s_id not in
(select stu.s_id
from te
left join co on te.t_id=co.t_id
left join sc on co.c_id=sc.c_id
left join stu on sc.s_id=stu.s_id
where te.t_name= ‘张三’);


– 14、练习:查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_name from stu where s_id not in
(select stu.s_id
from te
left join co on te.t_id=co.t_id
left join sc on co.c_id=sc.c_id
left join stu on sc.s_id=stu.s_id
where te.t_name=‘张三’);


– 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
– 思路:先找出学过01或02的,再找出学过课程数量为2的
select stu.*
from sc
left join stu on sc.s_id=stu.s_id
where c_id in (‘01’,‘02’)
group by stu.s_id
having count(c_id)=2;


– 10、练习:查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
– 思路:先找出01或02,再排除02
– group_concat的使用
select stu.* from sc
left join stu on sc.s_id=stu.s_id
where c_id in (‘01’,‘02’)
group by sc.s_id
having group_concat(c_id order by c_id)=‘01’;


– 45、查询选修了全部课程的学生信息
select count(c_id) from co;

select stu.*
from sc left join stu on sc.s_id=stu.s_id
group by sc.s_id
having count(c_id)=(select count(c_id) from co);


– 11、练习:查询没有学全所有课程的同学的信息
select * from stu where s_id not in
(select stu.s_id
from sc
left join stu on sc.s_id=stu.s_id
group by sc.s_id
having count(c_id)=(select count(c_id) from co));


– 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select c_id from sc where s_id=‘01’;

select distinct stu.*
from sc left join stu on sc.s_id=stu.s_id
where c_id in (select c_id from sc where s_id=‘01’)
group by sc.s_id;


– 13、练习:查询和"01"号的同学学习的课程完全相同的其他同学的信息
select group_concat(c_id) from sc where s_id=‘01’;

select stu.s_id,group_concat(c_id order by c_id)
from sc left join stu on sc.s_id = stu.s_id
group by stu.s_id
having group_concat(c_id order by c_id)=(select group_concat(c_id) from sc where s_id=‘01’) and stu.s_id<>‘01’;


– 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select s_id from sc where score<60 group by s_id having count(c_id)>=2;

select stu.s_id,s_name,avg(score)
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id
having stu.s_id in (select s_id from sc where score<60 group by s_id having count(c_id)>=2);


– 16、检索"01"课程分数小于60,按分数降序排列的学生信息
select stu.*
from sc left join stu on sc.s_id=stu.s_id
where c_id=‘01’ and score<60
order by score desc;


– 34、练习:查询课程名称为"数学",且分数低于60的学生姓名和分数
select s_name,score
from sc left join co on sc.c_id=co.c_id
left join stu on sc.s_id=stu.s_id
where c_name=‘数学’ and score<60;


– 38、练习:查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select stu.s_id,s_name
from sc left join stu on sc.s_id=stu.s_id
where c_id=‘01’ and score>=80;


– 35、查询所有学生的课程及分数情况(一维转二维)
– 重要学习点

select stu.s_id,
sum(if(c_id=‘01’,score,0)) ‘01’,
sum(if(c_id=‘02’,score,0)) ‘02’,
sum(if(c_id=‘03’,score,0)) ‘03’
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id;

select stu.s_id,
sum(case when c_id=‘01’ then score else 0 end) ‘01’,
sum(case when c_id=‘02’ then score else 0 end) ‘02’,
sum(case when c_id=‘03’ then score else 0 end) ‘03’
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id;

– 工作中使用最多的方法
select stu.s_id,
sum((c_id=‘01’)*score) ‘01’,
sum((c_id=‘02’)*score) ‘02’,
sum((c_id=‘03’)*score) ‘03’
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id;


– 17、练习:按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select stu.s_id,
sum((c_id=‘01’)*score) ‘01’,
sum((c_id=‘02’)*score) ‘02’,
sum((c_id=‘03’)*score) ‘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;


– 18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
#及格为:>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select sc.c_id,c_name,
max(score) 最高分,min(score) 最低分,avg(score) 平均分,
sum(score>=60 and score<70)/(select count(s_id) from stu) 及格占比,
sum(score>=70 and score<80)/(select count(s_id) from stu) 中等占比,
sum(score>=80 and score<90)/(select count(s_id) from stu) 优良占比,
sum(score>=90)/(select count(s_id) from stu)优秀占比
from sc left join co on sc.c_id=co.c_id
group by sc.c_id;


– 23、练习:统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select sc.c_id,c_name,
sum(score>=85)/(select count(s_id) from stu) ‘[100-85占比]’,
sum(score>=70 and score<85)/(select count(s_id) from stu) ‘[85-70占比]’,
sum(score>=60 and score<70)/(select count(s_id) from stu) ‘[70-60占比]’,
sum(score<60)/(select count(s_id) from stu) ‘[0-60占比]’,
sum(score>=85) ‘[100-85]’,
sum(score>=70 and score<85) ‘[85-70]’,
sum(score>=60 and score<70) ‘[70-60]’,
sum(score<60) ‘[0-60]’
from sc left join co on sc.c_id=co.c_id
group by sc.c_id;


– 19、查询学生的总成绩并进行排名

总成绩

select stu.s_id,sum(score) 总成绩
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id
order by sum(score) desc;

定义用户变量

set @r=0;
select @r;

循环计算

set @r=0;
select *,@r:=@r+1 排名
from
(select stu.s_id,sum(score) 总成绩
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id
order by sum(score) desc) t;


– 24、练习:查询每个学生平均成绩及其名次
set @r=0;
select *,@r:=@r+1 名次
from
(select stu.s_id,avg(score) 平均成绩
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id
order by avg(score) desc) p;

用一条语句执行排名

select *,@r:=@r+1 名次
from
(select stu.s_id,avg(score) 平均成绩
from stu left join sc on stu.s_id=sc.s_id
group by stu.s_id
order by avg(score) desc) p1,(select @r=0) p2;


– 20、练习:按各科成绩进行排序,并显示排名
– 工作中实践出来的方法·
set @c=null;
set @r=0;

select *,if(c_id=@c,@r:=@r+1,@r:=1) 排名,@c:=c_id 课程编号
from sc
order by c_id,score desc;

合并成一条语句

select *,if(@c=c_id,@r:=@r+1,@r:=1) 排名,@c:=c_id 课程编号
from sc,(select @c=null, @r=0) p
order by c_id,score desc;


– 21、练习:查询不同老师所教不同课程平均分 从高到低显示
select te.t_id,t_name,c_name,avg(score)
from te left join co on te.t_id=co.t_id
left join sc on sc.c_id=co.c_id
group by te.t_id
order by avg(score) desc;


– 25、查询各科成绩前三名的记录
– 难度比较高的重点
select *
from (select *,if(@c=c_id,@r:=@r+1,@r:=1) 排名,@c:=c_id 课程编号
from sc,(select @c=null, @r=0) p
order by c_id,score desc) t
where 排名<=3;

关联子查询

select *
from sc t1
where (select sum(t2.score >t1.score) from sc t2 where t1.c_id=t2.c_id)❤️
order by c_id,score desc;


– 42、练习:查询每门功成绩最好的前两名
select *
from sc t1
where (select sum(t2.score>t1.score) from sc t2 where t1.c_id=t2.c_id)<2
order by c_id,score desc;


– 22、练习:查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select *
from sc t1
where (select sum(t2.score>t1.score) from sc t2 where t1.c_id=t2.c_id)❤️
and (select sum(t2.score>t1.score) from sc t2 where t1.c_id=t2.c_id)>0
order by t1.c_id,t1.score desc;


– 26、查询每门课程被选修的学生数
select c_id,count(s_id)
from sc
group by c_id;


– 39、练习:求每门课程的学生人数
select c_id,count(s_id)
from sc
group by c_id;


– 43、练习:统计每门课程的学生选修人数(超过5人的课程才统计)
#要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c_id,count(s_id)
from sc
group by c_id
having count(s_id)>5
order by count(s_id) desc,c_id;


– 44、练习:检索至少选修两门课程的学生学号
select s_id
from sc
group by s_id
having count(c_id)>=2;


– 27、练习:查询出只有两门课程成绩的全部学生的学号和姓名
select sc.s_id,s_name
from sc left join stu on sc.s_id=stu.s_id
group by sc.s_id
having count(c_id)=2;

select t.s_id,s_name
from (select s_id from sc group by s_id having count(c_id)=2)t
left join stu on t.s_id=stu.s_id;


– 28、练习:查询男生、女生人数
select s_sex,count(s_sex)
from stu
group by s_sex;


– 30、查询同名同姓学生名单,并统计同名人数
select s_name,count(s_name)
from stu
group by s_name
having count(s_name)>1;


– 32、练习:查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select c_id,avg(score)
from sc
group by c_id
order by avg(score) desc,c_id;


– 36、练习:查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select * from sc where score>70;

select s_name,c_name,score
from (select * from sc where score>70) t left join stu on t.s_id=stu.s_id
left join co on t.c_id=co.t_id;


– 37、练习:查询出现过学生考试不及格的课程
select distinct c_id from sc where score<60;

select co.c_id,c_name
from co join (select distinct c_id from sc where score<60) t on co.c_id=t.c_id;


– 41、查询课程不同、成绩相同的学生的学生编号、课程编号、学生成绩
select *
from sc t1 where s_id in (select s_id from sc t2 where t1.s_id=t2.s_id and t1.c_id<>t2.c_id and t1.score=t2.score);


– 46、查询各学生的年龄
select s_name, (year(now())-year(s_birth)) 年龄
from stu;


– 31、练习:查询1990年出生的学生名单
select s_name
from stu
where year(s_birth)=‘1990’;


– 47、练习:查询本周过生日的学生
select *
from stu
where week(s_birth) = week(curdate());


– 48、练习:查询下周过生日的学生
select *
from stu
where week(s_birth)=if(week(curdate())=52,1,week(curdate())+1);


– 49、练习:查询本月过生日的学生
select *
from stu
where month(s_birth)=month(curdate());


– 50、练习:查询下月过生日的学生
select *
from stu
where month(s_birth)=if(month(curdate())=12,1,month(curdate()+1));

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值