sql经典练习50题

测试数据

--建表
--学生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
--课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
--教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
--成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
--插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
--课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

--教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

--成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
1. 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
select st.* 
from student st 
left join score sc on st.s_id=sc.s_id  and sc.c_id='01' 
left join  score sc1 on sc1.s_id=sc.s_id and sc1.c_id='02'
where sc.s_score>sc1.s_score;
select st.* 
from student st,score sc,score sc1 
where st.s_id=sc.s_id 
      and sc1.s_id=sc.s_id
      and sc.c_id='01' 
      and sc1.c_id='02' 
      and sc.s_score>sc1.s_score;
2. 查询同时存在" 01 “课程和” 02 "课程的学生的信息
select st.* 
from student st,score sc,score sc1 
where st.s_id=sc.s_id 
    and sc1.s_id=sc.s_id 
    and sc.c_id='01' 
    and sc1.c_id='02';
select st.* 
from student st 
left join score sc on st.s_id=sc.s_id  
left join score sc1 on sc1.s_id=sc.s_id  
where sc.c_id='01' and sc1.c_id='02';
select st.* 
from student st 
where st.s_id 
in(select sc.s_id from score sc where sc.c_id='01') 
and st.s_id in(select sc.s_id from score sc where sc.c_id='02');
select st.* 
from student st 
where st.s_id 
in(select sc.s_id from score sc where sc.c_id='02' or sc.c_id='01' 
    group by sc.s_id having count(sc.s_id)=2);
3. 查询存在" 01 “课程但可能不存在” 02 "课程的学生信息(不存在时显示为 null )
select st.*,sc.c_id '01课程',sc1.c_id '02课程' 
from student st 
left join score sc on sc.s_id=st.s_id and sc.c_id='01' 
left join score sc1 on sc1.s_id=st.s_id and sc1.c_id='02' 
where sc.c_id is not null;

4. 查询不存在" 01 “课程但存在” 02 "课程的学生信息
select st.* 
from student st 
where st.s_id in(select sc.s_id from score sc where sc.c_id='02') 
and st.s_id not in(select sc.s_id from score sc where sc.c_id='01');
5、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
select st.s_id,st.s_name,(case when ROUND(AVG(sc.s_score),2) is null then 0 else ROUND(AVG(sc.s_score),2) end ) "平均成绩" 
from student st
left join score sc on sc.s_id=st.s_id
group by st.s_id having AVG(sc.s_score)<60 or AVG(sc.s_score) is NULL
6. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select st.*,AVG(sc.s_score) '平均成绩' 
from student st 
left join score sc on st.s_id=sc.s_id 
group by st.s_id 
having avg(sc.s_score)>=60;
7. 查询在 student表存在成绩的学生信息
 select st.* 
 from student st 
 where st.s_id in (select sc.s_id 
                   from score sc where sc.s_score is not null);
8. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select st.s_id,st.s_name,count(sc.c_id) '选课总数',sum(sc.s_score) '总成绩'  
from student st 
left join score sc on st.s_id=sc.s_id 
group by st.s_id;
9. 查询「李」姓老师的数量
select count(t.t_id) 
from teacher t 
where t_name like '李%';
select t.t_name,count(t.t_id) 
from teacher t
group by t.t_id 
having t.t_name like "李%"; 
10. 查询学过「张三」老师授课的同学的信息
select st.* 
from student st 
where st.s_id in (select sc.s_id 
         from score sc 
         where sc.c_id in(select c.c_id 
                   from course c 
                   where c.t_id=(select t.t_id 
                         from teacher t 
                         where t.t_name='张三')));
select st.* 
from student st 
left join score sc on sc.s_id=st.s_id 
left join course c on c.c_id=sc.c_id 
left join teacher t on t.t_id=c.t_id 
where t.t_name='张三';
11. 查询没有学全所有课程的同学的信息
select st.* 
from student st 
where st.s_id in(select sc.s_id from score sc group by sc.s_id 
                 having count(sc.c_id)<(select count(c.c_id) from course c));
12. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select st.* 
from student st 
where st.s_id in (select sc1.s_id from score sc1 where sc1.c_id in  
                 (select sc.c_id from score sc where s_id='01'));
select st.* 
from student st 
left join score sc on sc.s_id=st.s_id 
where sc.c_id in(select c_id from score where s_id='01')
group by sc.s_id;
13.查询和"01"号的同学学习的课程完全相同的其他同学的信息

分析:查询出的结果要满足两个条件,首先是选修的课程数目要与01学生相一致
第二,选修了01学生没有选修的课不在结果集中
1.查询出编号‘01’的学生选修的全部课程:
select c_id from score where s_id=‘01’
2.在查询出01学生没有选修的课程:
select c.c_id from course c where c.c_id not in ( select c_id from score where s_id=‘01’)
3.查询出所有选修了01学生没有选修的课的学生
select sc1.s_id from score sc1 where sc1.c_id in (select c.c_id from course c where c.c_id not in (select c_id from score where s_id=‘01’))
4.查询出没有选修01学生没有选修的课的学生
6.查询出所有选修课程数量等于01学生选修课数量的

select s.* 
from student s
where s.s_id not inselect sc1.s_id 
                      from score sc1 
                      where sc1.c_id in (select c.c_id 
                                         from course c 
                                         where c.c_id not in (select c_id from score where s_id='01')
                                       )
                    ) 
and s.s_id in (select s1.s_id 
               from student s1 
               left join score scc on s1.s_id=scc.s_id 
               group by s1.s_id 
               having count(s1.s_id)=(select count(s_id) from score where s_id='01'));

第二种写法:
运用group_concat()函数

select  st.* from student st 
left join score sc on sc.s_id=st.s_id
group by st.s_id
having group_concat(sc.c_id) = 
(
select  group_concat(sc2.c_id) from student st2
left join score sc2 on sc2.s_id=st2.s_id
where st2.s_id ='01'
)
14. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select st.s_name
from student st 
where st.s_id 
not in(select sc.s_id from score sc 
       where c_id in(select c.c_id from course c 
                     where c.t_id=(select t.t_id from teacher t where t.t_name='张三')
                    )
         );
select st.s_name
from student st 
where st.s_id 
not in(select sc.s_id 
        from score sc 
        left join course c on c.c_id=sc.c_id 
        left join teacher t on t.t_id=c.t_id 
        where t.t_name='张三');
15. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select st.s_id,st.s_name,avg(sc1.s_score) 
from student st 
left join score sc1 on sc1.s_id=st.s_id 
where sc1.s_id in 
    (select sc.s_id from score sc 
     where sc.s_score<60 
     group by sc.s_id 
     having count(sc.s_id)>=2) 
group by st.s_id;
16. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select st.* 
from student st 
left join score sc on sc.s_id=st.s_id 
where sc.c_id='01' and sc.s_score<60 
order by sc.s_score desc;
select st.*,sc.s_score 
from student st 
inner join score sc on sc.s_id=st.s_id and sc.c_id="01" and sc.s_score<60
order by sc.s_score desc;
17. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select st.s_id,st.s_name,avg(sc.s_score) '平均分' ,sc1.s_score '语文',sc2.s_score '数学',sc3.s_score '英语' 
from student st 
left join score sc on sc.s_id=st.s_id 
left join score sc1 on sc1.s_id=st.s_id 
left join score sc2 on sc2.s_id=st.s_id 
left join score sc3 on sc3.s_id=st.s_id  
group by st.s_id 
order by avg(sc.s_score) desc;
18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select c.c_id,c.c_name,max(sc.s_score) "最高分",MIN(sc2.s_score) "最低分",avg(sc3.s_score) "平均分" 
,((select count(s_id) from score where s_score>=60 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "及格率"
,((select count(s_id) from score where s_score>=70 and s_score<80 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "中等率"
,((select count(s_id) from score where s_score>=80 and s_score<90 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "优良率"
,((select count(s_id) from score where s_score>=90 and c_id=c.c_id )/(select count(s_id) from score where c_id=c.c_id)) "优秀率"
from course c
left join score sc on sc.c_id=c.c_id 
left join score sc2 on sc2.c_id=c.c_id 
left join score sc3 on sc3.c_id=c.c_id 
group by c.c_id
19、按各科成绩进行排序,并显示排名
20、查询学生的总成绩并进行排名
select st.s_id,st.s_name, case when sum(sc.s_score) is null then 0 else sum(sc.s_score) end 
from student  st 
left join score sc on st.s_id=sc.s_id 
group by st.s_id 
order by sum(sc.s_score);
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
select s1.* 
from score s1 
where(select count(1) 
      from score s2 
      where s1.c_id=s2.c_id and s2.s_score>s1.s_score
      )<3
order by s1.c_id,s1.s_score desc;
SELECT
	a.s_id,
	a.c_id,
	a.s_score 
FROM
	score a
	LEFT JOIN score b ON a.c_id = b.c_id 
	AND a.s_score < b.s_score
GROUP BY
	a.s_id,
	a.c_id,
	a.s_score 
HAVING
	COUNT( b.s_id ) < 3
ORDER BY
	a.c_id,
	a.s_score DESC;

成绩相同,只选前三名

(select * from score where c_id='01' order by s_score desc limit 3) 
union 
(select * from score where c_id='02' order by s_score desc limit 3) 
union 
(select * from score where c_id='03' order by s_score desc limit 3);
26、查询每门课程被选修的学生数
select c_id,count(*) 
from score 
group by c_id;
27、查询出只有两门课程的全部学生的学号和姓名
select * 
from student st 
left join score sc on st.s_id=sc.s_id 
group by st.s_id 
having count(st.s_id)=2;
select * 
from student st
where st.s_id 
in(select sc.s_id from score sc group by s_id having count(s_id)=2);
28、查询男生、女生人数
select s_sex,count(*) 
from student  
group by s_sex;
29、查询名字中含有"风"字的学生信息
select * 
from student 
where s_name like '%风%';
30、查询同名同性学生名单,并统计同名人数
select s_name,count(s_name) 
from student 
group by s_name
having count(s_name)>1;
31、查询1990年出生的学生名单
select * 
from student 
where s_birth like '1990%';
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select sc.c_id,avg(sc.s_score) 
from score sc 
group by sc.c_id 
order by  avg(sc.s_score) desc, sc.c_id asc;
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select st.* 
from student st,(select s_id,avg(s_score) from score group by s_id having avg(s_score)>=85) ss where ss.s_id=st.s_id;
SELECT st.s_id,st.s_name,AVG(sc.s_score)'mean' FROM 
student as st
INNER JOIN
score as sc on st.s_id=sc.s_id
GROUP BY s_id HAVING mean>=85 ;
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select st.s_name,sc.s_score 
from student st 
left join score sc on st.s_id=sc.s_id 
where sc.s_score<60 and sc.c_id=(select c_id from course where c_name='数学');
35、查询所有学生的课程及分数情况;
select st.s_id,st.s_name,sc.c_id,sc.s_score 
from student st 
left join score sc on st.s_id=sc.s_id;
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
select st.s_id,st.s_name,sc.c_id,sc.s_score 
from student st 
left join score sc on st.s_id=sc.s_id 
where sc.s_score>70;
37、查询所有不及格的课程的学生信息
select distinct st.* 
from student st 
left join score sc on st.s_id=sc.s_id where sc.s_score<60;
select * 
from student 
where s_id in(select s_id  from score where s_score<60);
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select st.s_id,st.s_name 
from student st 
left join score sc on st.s_id=sc.s_id and sc.c_id='01' where sc.s_score>80;
39、求每门课程的学生人数
select sc.c_id,count(sc.c_id) 
from score sc 
group by sc.c_id;
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
 select * 
 from student st 
 left join score sc on st.s_id=sc.s_id
 where sc.c_id =(select c_id 
                 from course 
                 where t_id=(select t_id from teacher where t_name='张三')
                 ) 
order by sc.s_score desc limit 1;
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select distinct * 
from score sc 
left join score sc1 on sc.c_id<>sc1.c_id and sc.s_id<>sc1.s_id 
where sc.s_score =sc1.s_score;
42、查询每门功课成绩最好的前两名

自己与自己连接,其实就是列出同一门课内所有分数比较的情况。可以确定这个同学的这门课的这个分数究竟比多少个其他记录高/低,如果出现在这张表里的次数少于2个,那就意味着在这门课里排名前二的。

考虑并列成绩相同的情况:

select s1.* 
from score s1 
where(select count(1) 
      from score s2 
      where s1.c_id=s2.c_id and s2.s_score>s1.s_score
      )<2
order by s1.c_id,s1.s_score desc;
SELECT
	a.s_id,
	a.c_id,
	a.s_score 
FROM
	score a
	LEFT JOIN score b ON a.c_id = b.c_id 
	AND a.s_score < b.s_score
GROUP BY
	a.s_id,
	a.c_id,
	a.s_score 
HAVING
	COUNT( b.s_id ) < 2
ORDER BY
	a.c_id,
	a.s_score DESC;

成绩相同,只选前两名

(select * from score where c_id='01' order by s_score desc limit 2) 
union 
(select * from score where c_id='02' order by s_score desc limit 2) 
union 
(select * from score where c_id='03' order by s_score desc limit 2);
43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
select sc.c_id,count(sc.c_id) cidcount 
from score sc 
group by sc.c_id 
having cidcount>5 
order by cidcount desc;
44、检索至少选修两门课程的学生学号
select sc.s_id 
from score sc 
group by sc.s_id 
having count(sc.s_id)>=2;
45、查询选修了全部课程的学生信息
select * 
from student s 
where s.s_id 
in(select sc.s_id 
    from score sc 
    group by sc.s_id 
    having count(sc.s_id)=(select count(c_id) from course)
  );
46、查询各学生的年龄
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询下月过生日的学生
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值