综合练习题

表结构

整合一下方便查看

teacher

 student

 course

scors

练习题

1、自行创建测试数据

create table student(
sid int primary key auto_increment,
sname char(32),
gender enum("女","男"),
class_id int, constraint fk_class_id foreign key (class_id) references class(cid)
on DELETE cascade
on update cascade
); create table teacher(
tid int primary key auto_increment,
tname char(32)
); create table class(
cid int primary key auto_increment,
caption char(32)
); create table course(
cid int primary key auto_increment,
cname char(32),
teacher_id int,
constraint fk_teacher_id foreign key (teacher_id) references teacher(tid)
on delete cascade
on update cascade
); create table score(
sid int primary key auto_increment,
student_id int,
corse_id int,
number int,
constraint fk_student_id foreign key (student_id) references student(sid)
on delete cascade
on update cascade, constraint fk_corse_id foreign key (corse_id) references score(sid)
on delete cascade
on update cascade
);

2、查询“体育”课程比“数学”课程成绩高的所有学生的学号;

select a.student_id
from (select * from score where corse_id = (select cid from course where cname = '数学'))a
inner join (select * from score where corse_id = (select cid from course where cname = '体育'))b
on a.student_id = b.student_id and a.number > b.number;
select a.student_id
from
(select score.student_id, score.number
from score
left join course c on score.corse_id = c.cid
where c.cname = '体育') a inner join (select score.student_id, score.number
from score
left join course c on score.corse_id = c.cid
where c.cname = '数学') b on a.student_id = b.student_id and a.number < b.number;

3、查询平均成绩大于60分的同学的学号和平均成绩;

select student_id, AVG(number)AS avg
from score
group by student_id
having avg > 60;

4、查询所有同学的学号、姓名、选课数、总成绩;

select student.sid, sname, count(*),sum(number)
from student
left join score s on student.sid = s.student_id
group by student.sid;

5、查询姓“长”的老师的个数;

select count(*) from teacher where tname like "长%" ;

6、查询没学过“教授”老师课的同学的学号、姓名;

select s.sid,s.sname
from score
inner join student s on score.student_id = s.sid
inner join course c on score.corse_id = c.cid
inner join teacher t on c.teacher_id = t.tid
where t.tname <> '教授'
group by s.sid

7、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;

select s.sid,s.sname
from score
inner join student s on score.student_id = s.sid
inner join course c on score.corse_id = c.cid
where c.cid in (1,2)
group by s.sid

8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select distinct s.sname
from score
left join student s on score.student_id = s.sid
where corse_id not in (select cid from course
left join teacher t on course.teacher_id = t.tid where tname = '教授')

9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

思路
1 拿到成绩表中,课程为1 的记录
2 成绩表链表学生表 作为一个记录 3 重复上面步骤拿到课程为2 的链表记录 两个记录再基于相同的学生id链表。条件设置为 成绩比较
select distinct a.sid,a.sname
from
(select distinct sc.number, st.sname, st.sid
from score as sc
inner join student st on sc.student_id = st.sid
where corse_id = 2) a inner join (select distinct sc.number, st.sname, st.sid
from score as sc
inner join student st on sc.student_id = st.sid
where corse_id = 1) b on a.sid = b.sid where a.number < b.number

10、查询有课程成绩小于60分的同学的学号、姓名;

select distinct st.sid, st.sname
from student as st
inner join score s on st.sid = s.student_id
where s.number < 60

11、查询没有学全所有课的同学的学号、姓名;

思路
1 查询出来所有的课程然后计数
2 成绩表按照学生分组后统计记录个数
3 小于总课程个数即为没学全
4 相反 等于 则为学全
select st.sid, st.sname
from student as st
inner join score sc on st.sid = sc.student_id
group by st.sid
having count(*) < (select count(cid) from course)

12、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;

思路
1 先拿到 1 同学所学的所有课程的id
1.1 要链表 student 和 course 以及 score
2 将1同学的课程列表当做条件来判断是否在里面即可
  2.1 因为要查其他学生,排除掉 1 同学
from student as st
inner join score sc on st.sid = sc.student_id
inner join course as co on sc.corse_id = co.cid
where st.sid <> 1
and sc.corse_id in
(select co.cid
from student as st
inner join score sc on st.sid = sc.student_id
inner join course as co on sc.corse_id = co.cid
where st.sid = 1)

13、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;

同上,只是不需要再排除 1 同学

14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

同上上,再条件判断的时候改为完全等于

15、删除学习“长老”老师课的成绩表记录;

delete sc
from score as sc
inner join course as co on sc.sid = co.cid
inner join teacher te on co.teacher_id = te.tid
where tname = '长老'

16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;

17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

20、课程平均分从高到低显示(显示任课老师);

select co.cname, te.tname, avg(sc.number) av
from score as sc,
course as co,
teacher as te
where sc.corse_id = co.cid
and co.teacher_id = te.tid
group by sc.corse_id
order by av desc

21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

22、查询每门课程被选修的学生数;

23、查询出只选修了一门课程的全部学生的学号和姓名;

24、查询男生、女生的人数;

select count(*) from student where gender ='男';
select count(*) from student where gender ='女';

25、查询姓“张”的学生名单;

select * from student where sname like '萌%'

26、查询同名同姓学生名单,并统计同名人数;

select sname,count(sname) from student group by sname

27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

select co.cname, avg(sc.number) as av
from score as sc,
course as co
where sc.corse_id = co.cid
group by sc.corse_id
order by av asc, sc.corse_id desc

28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;

select st.sname, avg(sc.number)
from student as st,
score as sc
where st.sid = sc.student_id
group by st.sid

29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;

select st.sname, avg(sc.number) as av
from student as st
inner join score as sc on st.sid = sc.student_id
inner join course as co on sc.corse_id = co.cid
where co.cname = '数学'
group by st.sid
having av < 60

30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

select st.sid, st.sname
from student as st
inner join score sc on st.sid = sc.student_id
where sc.corse_id = 3
and sc.number > 80

31、求选了编号 ‘1 ’课程的学生人数

select count(*)
from student as st
inner join score sc on st.sid = sc.student_id
where sc.corse_id = 1

32、查询选修“长老”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

select st.sid, st.sname, max(sc.number) as max
from student as st,
score as sc,
course as co,
teacher as te
where sc.corse_id = co.cid
and st.sid = sc.student_id
and co.teacher_id = te.tid
and te.tname = '长老'

33、查询各个课程及相应的选修人数;

select co.cname,count(*)
from score as sc
inner join course as co on sc.corse_id = co.cid
group by sc.corse_id

34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

35、查询每门课程成绩最好的前两名;

select *
from score sc
where 2>(select count(*) from score where sid = sc.sid and number > sc.number)
order by sc.sid,sc.number desc

36、检索至少选修两门课程的学生学号;

select student.sname from score,student
where student_id = student.sid
group by student_id
having count(*) >1

37、查询全部学生都选修的课程的课程号和课程名;

select co.cid, co.cname
from student as st,
score as sc,
course as co
where sc.corse_id = co.cid
and st.sid = sc.student_id
group by sc.corse_id
having count(*) = (select count(*) from student)

38、查询没学过“长老”老师讲授的任一门课程的学生姓名;

39、查询两门以上不及格课程的同学的学号及其平均成绩;

select st.sid, st.sname, avg(sc.number) as av
from student as st,
score as sc,
course as co
where sc.corse_id = co.cid
and st.sid = sc.student_id
and sc.number < 60
having count(*) > 2

40、检索“003”课程分数小于60,按分数降序排列的同学学号;

select st.sid
from score as sc
inner join course as co on sc.corse_id = co.cid
inner join student as st on sc.student_id = st.sid
where sc.number < 60
order by sc.number desc

41、删除“002”同学的“001”课程的成绩;

delete sc
from score as sc,
student as st
where sc.student_id = st.sid
and st.sid = 2
and sc.corse_id = 1

MySQL 练习题 附答案,未完的更多相关文章

  1. MySQL练习题参考答案

    MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...

  2. s15day12作业:MySQL练习题参考答案

    MySQL练习题参考答案   导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径           # 结构+数据 mysqldump -u用户名 -p ...

  3. python 全栈开发,Day65(MySQL练习题,参考答案)

    一.MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再 ...

  4. MySQL练习题及答案(复习)

    新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...

  5. Mysql 练习题 及 答案

    --1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表  Course(C,Cname,T ...

  6. FCRA题库及答案(未完待续)

    一.FCRA-入门基础(23) 1.FineReport中普通模板总共有5种不同的预览模式:分页预览.填报预览.新填报预览.数据分析.移动端预览 决策报表下为表单预览.移动端预览 2.FineRepo ...

  7. 数据库---MySQL练习题及答案

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  8. MySQL练习题及答案

    一.现有三张数据库表,分别为部门表.员工表.部门和员工关系表 1.部门表CREATE TABLE `t_dept` ( `id` int(8) NOT NULL AUTO_INCREMENT, `de ...

  9. 完整项目:网上图书商城(一、MySQL数据库设计)未完

    一.建立数据库 CREATE DATABASE IF NOT EXISTS bookshop CHARACTER utf8; 二.建立数据库表 1.建立用户表 #用户表(用户id号,用户名,用户密码, ...

随机推荐

  1. @RequestBody ajax 415 400

    使用springmvc和Ajax进行数据交互时使用标签@RequestBody时我报了这两个错,刚开始对springmvc的使用和注解有点迷,然后踩坑上了. 先说下怎么才会踩上去.首先@Request ...

  2. MySQL复制表的方式以及原理和流程

    复制表的俩种方式: 第一.只复制表结构到新表 create table 新表 select * from 旧表 where 1=2 或者 create table 新表 like 旧表 第二.复制表结 ...

  3. centos 7 network.service control process exited

    一.service network restart 出错 问题描述: vmware 12 下centos 7 网络模式,NAT 昨晚作者打算更新自己虚拟机python,发现没网络ping www.ba ...

  4. mysql 中Varchar 与char的区别

    一.字符与字节与编码关系 ASCII码中,一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间.一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制.最 ...

  5. 初用Ajax

    早就有学习Ajax的想法了,但每次拿起一本Ajax的书,翻了不到百页就学不下去了,里面讲的东西实在太多了,前面讲javaScript的内容看了好 几遍都记不住,也就没心思去看后面的内容:看Ajax案例 ...

  6. Vmware由于centos升级内核不可运行(C header files matching your running kernel were not found)的解决方案

    C header files matching your running kernel were not found. Refer to your distribution's documentati ...

  7. Python3练习题 026:求100以内的素数

    p = [i for i in range(2,100)] #建立2-99的列表 for i in range(3,100): #1和2都不用判断,从3开始     for j in range(2, ...

  8. K8S集群 NOT READY的解决办法 1.13 错误信息:cni config uninitialized

    今天给同事 一个k8s 集群 出现not ready了 花了 40min 才搞定 这里记录一下 避免下载 再遇到了 不清楚. 错误现象:untime network not ready: Networ ...

  9. oninput和onchange的区别

    菜鸟教程: oninput事件:HTML5标准事件 当用户向<input>中尝试输入时执行JavaScript: <input type="text" oninp ...

  10. 保存后自动格式化代码(vscode)

    痛点: 写项目的时候, 我们经常会拷贝一些代码, 每当拷贝过来都需要重新调整, 如果可以实现保存自动调整代码, 将会给我们带来很多的便利! 解决: 其实对于vscode来说, 实现这一点很容易. 我们 ...