sql综合练习题

一、表关系
年级表:class_grade
create table class_grade(gid int primary key auto_increment,gname varchar(20) not null);
insert into class_grade(gname) values('一年级'),('二年级'),('三年级');
班级表:class
create table class(cid int primary key auto_increment,caption varchar(30) not null,grade_id int not null,constraint class_name foreign key(grade_id)references class_grade(gid)on delete cascadeon update cascade);
insert into class(caption,grade_id) values('一年一班',1),('二年一班',2),('三年二班',3);学生表:student
create table student(sid int primary key auto_increment,sname varchar(20) not null,gender enum('',''),class_id int not null,constraint student_name foreign key(class_id)references class(cid)on delete cascadeon update cascade);
insert into student(sname,gender,class_id) values('乔丹','',1),('艾弗森','',1),('科比','',2);老师表:teacher
create table teacher(tid int primary key auto_increment,tname varchar(30) not null);
insert into teacher(tname) values('张三'),('李四'),('王五');课程表:course
create table course(cid int primary key auto_increment,cname varchar(30) not null,teacher_id int not null,constraint teacher_name foreign key(teacher_id)references teacher(tid)on delete cascadeon update cascade
);
insert into course(cname,teacher_id) values('生物',1),('体育',1),('物理',2);成绩表:score
create table score(sid int primary key auto_increment,student_id int not null,course_id int not null,score int not null,foreign key(student_id) references student(sid)on delete cascadeon update cascade,foreign key(course_id) references course(cid)on delete cascadeon update cascade
);insert into score(student_id,course_id,score) values(1,1,60),(1,2,59),(2,2,99);班级任职表:teach2cls
create table teach2cls(tcid int primary key auto_increment,tid int not null,cid int not null,foreign key(tid) references teacher(tid)on delete cascadeon update cascade,foreign key(cid) references class(cid)on delete cascadeon update cascade
);insert into teach2cls(tid,cid) values(1,1),(1,2),(2,1),(3,2);2、查询学生总人数
select count(sname) 总人数 from student;3、查询’生物‘课程和’物理‘课程成绩都及格的学生id和姓名
select sid,sname
fromstudent
inner join(select student_idfrom scorewherecourse_id in (selectcidfromcoursewherecname in ('生物','物理')) and  score >= 60) as t1 on t1.student_id = student.sid;4、查询每个年级的班级数,取出班级数最多的前三个年级;
selectclass_grade.gid,class_grade.gname
fromclass_grade
inner join
(select grade_id,count(cid)fromclassgroup by grade_idorder by grade_id desclimit 3
) as t1 on t1.grade_id = class_grade.gid;5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
selectstudent.sid,student.sname,avg_score
fromstudent
inner join(selectstudent_id,avg(score) as avg_scorefromscoregroup bystudent_idhaving avg(score) in((selectavg(score) as max_scorefromscoregroup by student_idorder byavg(score) desclimit 1),(selectavg(score) as min_scorefromscoregroup bystudent_idorder byavg(score)limit 1))) as t1 on t1.student_id = student.sid;6、查询每个年级的学生人数
select class_grade.gname,count_cid
from class_grade
inner join(selectgrade_id,count(cid) as count_cidfromclass,studentwhereclass.cid = class_idgroup by grade_id) as t1 on t1.grade_id = class_grade.gid;7、查询每位学生的学号、姓名、选课数、平均成绩;
selectstudent.sid,student.sname,course_count,avg_score
fromstudent
left join(selectstudent_id,count(course_id) as course_count,avg(score) as avg_scorefromscoregroup by student_id) as t1 on t1.student_id = student.sid;8、查询学生编号为‘2’的学生的姓名,该学生成绩最高的课程名、成绩最低的课程名及分数;
selectstudent.sname,student.sid,t1.score
from
(selectstudent_id,course_id,scorefrom scorewhere student_id = 2 and score in((selectmax(score)fromscorewhere student_id = 2),(select min(score)fromscorewhere student_id =2)) 
)as t1
inner join student on t1.student_id = student.sid
inner join course on t1.course_id = course.cid;9、查询‘李’的老师的个数和所带班级数;
selectcount(teacher.tname) as '李%个数',count(teach2cls.cid) as '班级数量'
from teacher left join teach2cls on teach2cls.tid = teacher.tid
where teacher.tname like '李%';10、查询班级数小于5年级的id和年级名;
selectgid,gname,count(cid)
fromclass_grade
inner join class on gid = grade_id
group bygid
havingcount(cid) < 5
11、查询班级信息,包括班级id、班级名称、年级、年级级别(12
为低年级,34为中年级,56为高年级)
selectclass.cid as '班级id',class.caption as '班级名称',class_grade.gid as '年级',
casewhen class_grade.gid between 1 and 2 then ''when class_grade.gid between 3 and 4 then ''when class_grade.gid between 5 and 6 then '' else 0 end as '年级级别'
fromclass
left join class_grade on class_grade.gid=class.grade_id;12、查询学过“张三”老师2门课以上的同学的学号、姓名;
select sid,sname
fromstudent
where sid in 
(
selectstudent_id
fromscore
left join course on course_id = course.cid
where course.teacher_id in (    select tidfrom teacherwhere tname = '张三')
group bystudent_id
having count(course.cid)>=2
)13、查询教授课程超过2门的老师的id和姓名;
selecttid,tname
fromteacher
inner join (selectteacher_id,count(cid)fromcoursegroup byteacher_idhaving count(cid) >=2
) as t1 on t1.teacher_id = teacher.tid;14、查询学过编号‘1’课程和编号2课程的同学的学号、姓名;
selectsid,sname
fromstudent
where sid in 
(
select course_id
fromscore
group bycourse_id
havingcourse_id in (1,2)
)15、查询没有带过高年级的老师id和姓名;
selecttid,tname
fromteacher
where tid  in
(
select tid
fromteach2cls
where tid in 
(selectcid
fromclass
where grade_id in (5,6)));16、查询学过'张三'老师所教的所有课的同学的学号、姓名;
selectsid,sname
fromstudent
where sid in (selectstudent_idfromscorewhere course_id in (    selectcidfromcourseinner join teacher on teacher_id = teacher.tidwhere teacher.tname ='张三'));17、查询带过超过2个班级的老师的id和姓名;
selecttid,tname
fromteacher
where tid in (
selecttid
fromteach2cls
group bytid
havingcount(cid) >2);18、查询课程编号’2‘的成绩比课程’1‘课程低的所有同学的学号、姓名
select sid,sname
fromstudent
where sid in (
select t1.student_id
from
(selectstudent_id,scorefromscorewhere course_id =2
) as t1
inner join
(selectstudent_id,scorefromscorewhere course_id =1
) as t2 on t1.student_id = t2.student_id
where t1.score < t2.score);19、查询所带班级数最多的老师id和姓名;
selecttid,tname
fromteacher
where tid =
(selecttid
fromteach2cls
group bytid
order by count(cid) desc
limit 1);
20、查询有课程成绩小于60分的同学的学号、姓名;
selectsid,sname
fromstudent
where sid in (
selectstudent_id
fromscore
where score <60);
21、查询没有学全所有课的同学的学号、姓名;
selectsid,sname
fromstudent
where sid not in (
selectstudent_id
fromscore
group bystudent_id
havingcount(course_id) = (select count(cid) from course));
22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
selectsid,sname
fromstudent
where sid in 
(
selectstudent_id
fromscore
where course_id in 
(selectcourse_id
fromscore
where student_id = 1));23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
selectsid,sname
fromstudent
where sid in 
(
selectstudent_id
fromscore
where course_id in 
(selectcourse_id
fromscore
where student_id = 1)
havingstudent_id !=1
);24、查询和‘2’号同学学习的课程完全相同的其它同学的学号和姓名
selectsid,sname
fromstudent
where sid in 
(selectstudent_idfromscore,(select course_id fromscorewhere student_id = 2) as t1where score.course_id = t1.course_id and score.student_id !=2group byscore.student_idhavingcount(score.course_id)=(select count(course_id) from scorewhere student_id =2)
);
25、删除学习‘张三’老师课的score表记录;
delete
fromscore
where course_id in (
select cid
fromcourse
where course.teacher_id = (
select tid
from teacher
where teacher.tname = '张三'));26、向score表中插入一些记录,这些记录要求符合以下条件:
1、没有上过编号‘2’课程的同学学号
2、插入’2‘号课程的平均成绩insert into score(student_id,course_id,score) select t1.sid,2,t2.avg from (select sid from student where sid not in (select student_id from score where course_id = 2)) as t1,(select avg(score) as avg from score group by course_id having course_id =2) as t2;27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;select sc.student_id as  学生ID,(select score.score from score left join course on score.course_id = course.cid where course.cname = '生物' and score.student_id = sc.student_id) as 生物,(select score.score from score left join course on score.course_id = course.cid where course.cname = '体育' and score.student_id = sc.student_id) as 体育,(select score.score from score left join course on score.course_id = course.cid where course.cname = '物理' and score.student_id = sc.student_id) as 物理,count(sc.course_id) as '有效课程数',avg(sc.score) as '有效平均分'from score as sc group by sc.student_id order by avg(sc.score);28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,高低分;select course_id as "课程ID",max(score) as "最高分",min(score) as "最低分" from scoregroup by course_id29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;SELECT course_id as '课程ID',AVG(score) as '平均成绩',sum(CASE WHEN score > 60 then 1 ELSE 0 END)/COUNT(1)*100 as '及格率'from scoreGROUP BY course_id ORDER BY '平均成绩' ASC,'及格率' desc;30、课程平均分从高到低显示(现实任课老师);
SELECT score.course_id as '课程ID',avg(score) as '平均分' from score
inner join course on score.course_id = course.cid
GROUP BY course_id
ORDER BY avg(score) DESC31、查询各科成绩前三名的记录(不考虑成绩并列情况)
SELECT score.sid,score.course_id,score.score,t1.first_score,t1.sencond_score,t1.third_score from score LEFT JOIN
(SELECT sid,(select  score from score as s2 where s2.course_id=s1.course_id ORDER BY score desc LIMIT 0,1) as first_score,(select  score from score as s2 where s2.course_id=s1.course_id ORDER BY score desc LIMIT 1,1) as sencond_score,(select  score from score as s2 where s2.course_id=s1.course_id ORDER BY score desc LIMIT 2,1) as third_score
from score as s1)as t1 on score.sid = t1.sid
WHERE score.score in (t1.first_score,t1.sencond_score,t1.third_score)32、查询每门课程被选修的学生数;
SELECT score.course_id as '课程ID',count(student_id) as '学生数' from score
GROUP BY course_id33、查询选修了2门以上课程的全部学生的学号和姓名;
SELECT student.sid,student.sname from student WHERE sid in (
SELECT score.student_id from score
GROUP BY student_id
HAVING count(course_id) >=2);34、查询男生、女生的人数,按倒序排列;
select gender,count(sid) as sum from student
group by gender
order by sum desc35、查询姓“张”的学生名单;
SELECT sname from student WHERE sname like '张%'36、查询同名同姓学生名单,并统计同名人数;
SELECT sname as '名字',count(sname) as '同名人数' from student
GROUP BY sname
HAVING count(sname) >137、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select score.course_id,avg(score) as avg from score
INNER JOIN course on course.cid = course_id
GROUP BY course_id
ORDER BY avg ASC,course_id DESC;38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
SELECT sid,sname from student
where sid in (
SELECT score.student_id  from score
INNER JOIN course on course.cid = score.course_id
WHERE course.cname = '体育' and score.score <60);39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
SELECT sid,sname from  student
WHERE sid in (
SELECT score.student_id  from score
INNER JOIN course on course.cid = course_id
WHERE course_id = 3 and score.score > 80);40、求选修了课程的学生人数
select course_id as '课程ID',count(student_id) as '学生人数' from score group by course_id;41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
SELECT student.sname,max(score),min(score) from score
INNER JOIN student on score.student_id = student.sid
WHERE course_id in (
SELECT cid from course
WHERE teacher_id in (
SELECT tid FROM teacher
WHERE tname = '王五'))
GROUP BY student_id
ORDER BY max(score) DESC,MIN(score) ASC
LIMIT 242、查询各个课程及相应的选修人数;
SELECT score.course_id as 'ID',course.cname as '课程',count(student_id) as '人数' from score
LEFT JOIN course on score.course_id = course.cid
GROUP BY course_id43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
select DISTINCT s1.course_id,s2.course_id,s1.score,s2.score from score as s1, score as s2 where s1.score = s2.score and s1.course_id != s2.course_id;44、查询每门课程成绩最好的前两名学生id和姓名;
SELECT score.sid,score.course_id,score.score,t1.first_score,t1.sencond_score from score LEFT JOIN
(SELECT sid,(select  score from score as s2 where s2.course_id=s1.course_id ORDER BY score desc LIMIT 0,1) as first_score,(select  score from score as s2 where s2.course_id=s1.course_id ORDER BY score desc LIMIT 1,1) as sencond_score
from score as s1)as t1 on score.sid = t1.sid
WHERE score.score <= t1.first_score and score.score >= t1.sencond_score45、检索至少选修两门课程的学生学号;
SELECT score.student_id as '学生ID',count(course_id) as '课程ID' from score
GROUP BY student_id
HAVING count(course_id)>=246、查询没有学生选修的课程的课程号和课程名;
SELECT cid,cname from course
WHERE cid not IN(
SELECT score.course_id from score
GROUP BY score.course_id)47、查询没带过任何班级的老师id和姓名;
SELECT tid,tname from teacher
WHERE tid not in(
SELECT teach2cls.cid from teach2cls
GROUP BY teach2cls.cid)48、查询有两门以上课程超过80分的学生id及其平均成绩;
SELECT score.student_id as '学生ID',avg(score) as '平均成绩' from score
WHERE score > 30 
GROUP BY student_id
HAVING count(course_id) >=249、检索“3”课程分数小于60,按分数降序排列的同学学号;
select score.student_id from score
WHERE score < 60 and course_id = 3
ORDER BY score DESC50、删除编号为“2”的同学的“1”课程的成绩
delete from score where score.student_id = 2 and score.course_id = 1;51、查询同时选修了物理课和生物课的学生id和姓名;
SELECT sid,sname from student
WHERE sid in(
SELECT score.student_id from score
WHERE course_id in (
SELECT cid from course
WHERE course.cname in ('生物','物理'))
GROUP BY student_id
HAVING count(course_id) =2)

 

转载于:https://www.cnblogs.com/yjiu1990/p/9263395.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390156.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javascript原型_在JavaScript中冻结原型时会发生什么

javascript原型Have you wondered what happens when you freeze the prototype of an object? Lets find out together.您是否想过冻结对象的原型时会发生什么&#xff1f; 让我们一起找出答案。 对象 (Objects) In JavaScript, objects are dynamic collections of propert…

迟来的2017总结

明天就是年后第一天上班了&#xff08;过年期间请了6天假&#xff09;&#xff0c; 打算今天写一下2017的总结&#xff0c;本来还想写2018的愿景的&#xff0c;不过想想还是算了&#xff0c;现在没什么想法&#xff0c;不想敷衍了事。 先贴一个2017的提升计划&#xff1a; http…

tomcat启动卡住

新部署的项目启动tomcat后一直停在org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.16&#xff0c;卡在了org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/web…

怎样准备阿里技术面试_如何准备技术面试

怎样准备阿里技术面试In June 2020 I watched an inspiring talk by Anthony D. Mays, a technical coach and founder at Morgan Latimerco. He came on a Facebook Developer Circles Benin live session and talked about how to prepare for a technical interview. 2020年…

通过一个简单例子理解 RecyclerView.ItemDecoration

一、前言 RecyclerView 是从5.0推出的 MD 风格的控件。RecyclerView 之前有 ListView、GridView&#xff0c;但是功能很有限&#xff0c;例如 ListView 只能实现垂直方向上的滑动等。但是存在则合理&#xff0c;ListView 却没有被官方标记为 Deprecated&#xff0c;有兴趣的同学…

Entity Framework Logging and Intercepting Database Operations (EF6 Onwards)

参考官方文档&#xff1a;https://msdn.microsoft.com/en-us/library/dn469464(vvs.113).aspx转载于:https://www.cnblogs.com/liandy0906/p/8473110.html

面试题 17.14. 最小K个数

面试题 17.14. 最小K个数 设计一个算法&#xff0c;找出数组中最小的k个数。以任意顺序返回这k个数均可。 示例&#xff1a; 输入&#xff1a; arr [1,3,5,7,2,4,6,8], k 4 输出&#xff1a; [1,2,3,4] 提示&#xff1a; 0 < len(arr) < 1000000 < k < min(1…

这是您现在可以免费获得的115张Coursera证书(在冠状病毒大流行期间)

At the end of March, the world’s largest Massive Open Online Course provider Coursera announced that they are offering 100 free courses in response to the impact of the COVID-19 pandemic. 3月底&#xff0c;全球最大的大规模在线公开课程提供商Coursera 宣布 &a…

由浅入深理解----java反射技术

java反射机制详解 java反射机制是在运行状态下&#xff0c;对任意一个类可以获取该类的属性和方法&#xff0c;对任意一个对象可以调用其属性和方法。这种动态的获取信息和调用对象的方法的功能称为java的反射机制 class<?>类&#xff0c;在java.lang包下面&#xff0c;…

【VMware vSAN 6.6】5.5.Update Manager:vSAN硬件服务器解决方案

目录 1. 简介 1.1.适用于HCI的企业级存储2. 体系结构 2.1.带有本地存储的服务器2.2.存储控制器虚拟系统套装的缺点2.3.vSAN在vSphere Hypervisor中自带2.4.集群类型2.5.硬件部署选项3. 启用vSAN 3.1.启用vSAN3.2.轻松安装3.3.主动测试4. 可用性 4.1.对象和组件安置4.2.重新构建…

5848. 树上的操作

给你一棵 n 个节点的树&#xff0c;编号从 0 到 n - 1 &#xff0c;以父节点数组 parent 的形式给出&#xff0c;其中 parent[i] 是第 i 个节点的父节点。树的根节点为 0 号节点&#xff0c;所以 parent[0] -1 &#xff0c;因为它没有父节点。你想要设计一个数据结构实现树里面…

了解如何通过Python使用SQLite数据库

SQLite is a very easy to use database engine included with Python. SQLite is open source and is a great database for smaller projects, hobby projects, or testing and development.SQLite是Python附带的非常易于使用的数据库引擎。 SQLite是开源的&#xff0c;是用于…

32位JDK和64位JDK

32位和64位系统在计算机领域中常常提及&#xff0c;但是仍然很多人不知道32位和64位的区别&#xff0c;所以本人在网上整理了一些资料&#xff0c;并希望可以与大家一起分享。对于32位和64位之分&#xff0c;本文将分别从处理器&#xff0c;操作系统&#xff0c;JVM进行讲解。 …

中小企业如何选择OA协同办公产品?最全的对比都在这里了

对于中小企业来说&#xff0c;传统的OA 产品&#xff0c;如泛微、蓝凌、致远、华天动力等存在价格高、使用成本高、二次开发难等特点&#xff0c;并不适合企业的协同管理。 国内OA市场也出现了一批轻便、低价的OA产品&#xff0c;本文针对以下几款适合中小企业的OA产品在功能、…

python缓冲区_如何在Python中使用Google的协议缓冲区

python缓冲区When people who speak different languages get together and talk, they try to use a language that everyone in the group understands. 当说不同语言的人聚在一起聊天时&#xff0c;他们会尝试使用小组中每个人都能理解的语言。 To achieve this, everyone …

PowerDesigner16中的对象无效,不允许有扩展属性 问题的解决

PowerDesigner16中的对象无效&#xff0c;不允许有扩展属性 消息 15135&#xff0c;级别 16&#xff0c;状态 1&#xff0c;过程 sp_addextendedproperty&#xff0c;第 37 行 对象无效。XXXXXXX 不允许有扩展属性&#xff0c;或对象不存在。 把 execute sp_addextendedpropert…

Elasticsearch学习(2)—— 常见术语

为什么80%的码农都做不了架构师&#xff1f;>>> cluster (集群)&#xff1a;一个或多个拥有同一个集群名称的节点组成了一个集群。每个集群都会自动选出一个主节点&#xff0c;如果该主节点故障&#xff0c;则集群会自动选出新的主节点来替换故障节点。 node (节点…

67. 二进制求和

67. 二进制求和 给你两个二进制字符串&#xff0c;返回它们的和&#xff08;用二进制表示&#xff09;。 输入为 非空 字符串且只包含数字 1 和 0。 示例 1: 输入: a “11”, b “1” 输出: “100” 示例 2: 输入: a “1010”, b “1011” 输出: “10101” 提示&…

前端开发有哪些技术栈要掌握_为什么要掌握前端开发的这四个主要概念

前端开发有哪些技术栈要掌握After working as a front-end developer for three years, I have been able to summarize what I feel are the four major concepts of front-end development. Knowing and studying these four areas will make you stand out from the crowd.在…

python中的序列化与反序列化

之前&#xff0c;在学习python时&#xff0c;一直弄不明白pickle和json模块的序列化和反序例化之间的区别和用法&#xff0c;最近闲来有时间&#xff0c;重新研究了这两个模块&#xff0c;也算是基本搞明白他们之中的区别了。 用于序列化的两个模块&#xff0c; json&#xff0…