MySQL 练习题 附答案,未完
综合练习题
表结构
整合一下方便查看
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 练习题 附答案,未完的更多相关文章
- MySQL练习题参考答案
MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...
- s15day12作业:MySQL练习题参考答案
MySQL练习题参考答案 导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径 # 结构+数据 mysqldump -u用户名 -p ...
- python 全栈开发,Day65(MySQL练习题,参考答案)
一.MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再 ...
- MySQL练习题及答案(复习)
新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...
- Mysql 练习题 及 答案
--1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course(C,Cname,T ...
- FCRA题库及答案(未完待续)
一.FCRA-入门基础(23) 1.FineReport中普通模板总共有5种不同的预览模式:分页预览.填报预览.新填报预览.数据分析.移动端预览 决策报表下为表单预览.移动端预览 2.FineRepo ...
- 数据库---MySQL练习题及答案
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- MySQL练习题及答案
一.现有三张数据库表,分别为部门表.员工表.部门和员工关系表 1.部门表CREATE TABLE `t_dept` ( `id` int(8) NOT NULL AUTO_INCREMENT, `de ...
- 完整项目:网上图书商城(一、MySQL数据库设计)未完
一.建立数据库 CREATE DATABASE IF NOT EXISTS bookshop CHARACTER utf8; 二.建立数据库表 1.建立用户表 #用户表(用户id号,用户名,用户密码, ...
随机推荐
- Dijkstra的应用
每次只涉及一边两端点的极值循环转移应用Dijkstra.
- 使用matplotlib画饼图
import matplotlib.pyplot as pltx = [4, 9, 21, 55, 30, 18]labels = ['math', 'history', 'chemistry', ' ...
- python_函数名的应用、闭包、装饰器
0.动态传参内容补充: 0.1 单纯运行如下函数不会报错. def func1(*args,**kwargs): pass func1() 0.2 *的魔性用法 * 在函数定义的时候,代表聚合. *在 ...
- 实验楼----PHP代码审计(sha1、md5)
地址:http://www.shiyanbar.com/ctf/1787 题目:
- spring MVC请求处理类注解属性详解
- nginx反向代理proxy_pass的问题
起因:今天企业部署一个项目,用的nginx做的反向代理,配置如下: 测试结果令人失望,IP:端口 能访问项目,域名:端口 也能访问 ,但是 域名/接口名 访问失败 ################## ...
- mysql定时任务event——清理过期数据
需要删除数据的表名:t_req_log 建表sql CREATE TABLE `t_req_log` ( `id` ) NOT NULL AUTO_INCREMENT, `host` ) DEFAUL ...
- IWMS后台上传文章,嵌入音频文件代码
<object width="260" height="69" classid="clsid:6bf52a52-394a-11d3-b153-0 ...
- supervisor /var/run/supervisor/supervisor.sock not found 或者/tmp/supervisor.sock not found
刚按装完supervisor,这时候用supervisorctr -c supervisor.conf 会报错: /var/run/supervisor/supervisor.sock not fou ...
- vue 響應接口
全局方式: 增加屬性和set()和get(): vue.set(targname,key,vaule) targname:對象名或者數組名 key:字符串 value:任何值 刪除屬性和set()和g ...