mysql练习题4

文章目录

一、准备数据

#创建表及插入记录
CREATE TABLE class (
  cid int(11) NOT NULL AUTO_INCREMENT,
  caption varchar(32) NOT NULL,
  PRIMARY KEY (cid)
) ENGINE=InnoDB CHARSET=utf8;

INSERT INTO class VALUES
(1, '三年二班'), 
(2, '三年三班'), 
(3, '一年二班'), 
(4, '二年九班');

CREATE TABLE course(
  cid int(11) NOT NULL AUTO_INCREMENT,
  cname varchar(32) NOT NULL,
  teacher_id int(11) NOT NULL,
  PRIMARY KEY (cid),
  KEY fk_course_teacher (teacher_id),
  CONSTRAINT fk_course_teacher FOREIGN KEY (teacher_id) REFERENCES teacher (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO course VALUES
(1, '生物', 1), 
(2, '物理', 2), 
(3, '体育', 3), 
(4, '美术', 2);

CREATE TABLE score (
  sid int(11) NOT NULL AUTO_INCREMENT,
  student_id int(11) NOT NULL,
  course_id int(11) NOT NULL,
  num int(11) NOT NULL,
  PRIMARY KEY (sid),
  KEY fk_score_student (student_id),
  KEY fk_score_course (course_id),
  CONSTRAINT fk_score_course FOREIGN KEY (course_id) REFERENCES course (cid),
  CONSTRAINT fk_score_student FOREIGN KEY (student_id) REFERENCES student(sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO score VALUES
(1, 1, 1, 10),
(2, 1, 2, 9),
(5, 1, 4, 66),
(6, 2, 1, 8),
(8, 2, 3, 68),
(9, 2, 4, 99),
(10, 3, 1, 77),
(11, 3, 2, 66),
(12, 3, 3, 87),
(13, 3, 4, 99),
(14, 4, 1, 79),
(15, 4, 2, 11),
(16, 4, 3, 67),
(17, 4, 4, 100),
(18, 5, 1, 79),
(19, 5, 2, 11),
(20, 5, 3, 67),
(21, 5, 4, 100),
(22, 6, 1, 9),
(23, 6, 2, 100),
(24, 6, 3, 67),
(25, 6, 4, 100),
(26, 7, 1, 9),
(27, 7, 2, 100),
(28, 7, 3, 67),
(29, 7, 4, 88),
(30, 8, 1, 9),
(31, 8, 2, 100),
(32, 8, 3, 67),
(33, 8, 4, 88),
(34, 9, 1, 91),
(35, 9, 2, 88),
(36, 9, 3, 67),
(37, 9, 4, 22),
(38, 10, 1, 90),
(39, 10, 2, 77),
(40, 10, 3, 43),
(41, 10, 4, 87),
(42, 11, 1, 90),
(43, 11, 2, 77),
(44, 11, 3, 43),
(45, 11, 4, 87),
(46, 12, 1, 90),
(47, 12, 2, 77),
(48, 12, 3, 43),
(49, 12, 4, 87),
(52, 13, 3, 87);


CREATE TABLE student(
  sid int(11) NOT NULL AUTO_INCREMENT,
  gender char(1) NOT NULL,
  class_id int(11) NOT NULL,
  sname varchar(32) NOT NULL,
  PRIMARY KEY (sid),
  KEY fk_class (class_id),
  CONSTRAINT fk_class FOREIGN KEY (class_id) REFERENCES class (cid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO student VALUES
(1, '男', 1, '理解'), 
(2, '女', 1, '钢蛋'), 
(3, '男', 1, '张三'), 
(4, '男', 1, '张一'), 
(5, '女', 1, '张二'), 
(6, '男', 1, '张四'), 
(7, '女', 2, '铁锤'), 
(8, '男', 2, '李三'), 
(9, '男', 2, '李一'), 
(10, '女', 2, '李二'), 
(11, '男', 2, '李四'), 
(12, '女', 3, '如花'), 
(13, '男', 3, '刘三'), 
(14, '男', 3, '刘一'), 
(15, '女', 3, '刘二'), 
(16, '男', 3, '刘四');

CREATE TABLE teacher(
  tid int(11) NOT NULL AUTO_INCREMENT,
  tname varchar(32) NOT NULL,
  PRIMARY KEY (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO teacher VALUES
(1, '张磊老师'), 
(2, '李平老师'), 
(3, '刘海燕老师'), 
(4, '朱云海老师'), 
(5, '李杰老师');

二、题目

1、查询Score表中成绩在60到80之间的所有记录

select * from score where degree between 60 and 80;

2、查询 score 表中成绩为85,86或88的记录

select sno,cno,degree from score where degree in (85,86,88);

3、以 cno 升序、degree降序查询 score 表的所有记录

select * from score order by cno asc, degree desc;

4、查询“95031”班的学生人数。

select count(*) from student where class = '95031';

5、查询Score表中的最高分的学生学号和课程号’

## 子查询
select sno,cno from score where degree = (select max(degree) from score);
## 排序法
select sno,cno from score order by degree desc limit 1;

6、查询‘3-105’号课程的平均分。

select avg(degree) from score where cno = '3-105';

7、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

select avg(degree) from score where cno like '3%' group by cno having 
count(*) >= 5;

8、查询最低分大于70,最高分小于90的Sno列。

select sno from score group by sno having max(degree) between 70 and 90;

9、查询所有学生的Sname、Cno和Degree列

## 内连接查询
select stu.sname,sc.cno,sc.degree from student as stu
inner join score as sc on stu.sno = sc.sno;

## 多表查询
select sname,cno,degree from student,score where student.sno = score.sno;

10、查询所有学生的Sno、Cname和Degree列(参考答案疑似有误,不再做参考)

## 多表联查
select student.sno,cname,degree from student,course,score where
student.sno = score.sno and course.cno = score.cno;

## 连接查询
select stu.sno,c.cname,sc.degree from student as stu
inner join score as sc on sc.sno = stu.sno
inner join course as c on c.cno = sc.cno;

11、查询所有学生的Sname、Cname和Degree列。

## 多表联查
select student.sname,cname,degree from student,course,score where
student.sno = score.sno and course.cno = score.cno;

## 连接查询
select stu.sname,c.cname,sc.degree from student as stu
inner join score as sc on sc.sno = stu.sno
inner join course as c on c.cno = sc.cno;

12、查询“95033”班所选课程的平均分。

select avg(degree) from student as s
inner join score as sc on s.sno = sc.sno
group by class having class = '95033';

## 通过in 方式查询出的集合,再用聚合函数avg计算出集合中分数的平均分
select *,avg(degree) from score
where sno in (select sno from student where class = '95033');

## in 查询到的集合
select * from score
where sno in (select sno from student where class = '95033');

13、查询所有同学的Sno、Cno和rank列。

## 此题自由度较高,rank 有等级的意思,这里应该是查学生的成绩等级
## 使用case when
select sno,cno,
case when degree < 60 then 'D'
when degree <= 70 and degree >= 60 then 'C'
when degree <= 80 and degree > 70 then 'B'
when degree <= 100 and degree > 80 then 'A'
else '成绩不在正常范围内'
end
as rank from score;

14、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select * from score where cno = '3-105' and degree > 
(select degree from score where sno = '109' and cno = '3-105');

15、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录

## 查询出学生选学的数量
select count(*) from score group by sno;
## 正式查询
select * from score as s where (select count(*) from score as s2 where s.sno = s2.sno) >= 1 and
degree <> (select max(degree) from score as s2 where s.cno = s2.cno);

16、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select sno,sname,sbirthday from student where year(sbirthday) =
(select year(sbirthday) from student where sno = '108') and sno <> '108';

17、查询“张旭“教师任课的学生成绩。

select * from score where cno = (select cno from course where tno =
(select tno from teacher where tname = '张旭'));

## 连接查询
select s.sno,degree from score as s
inner join course as c on s.cno = c.cno
inner join teacher as t on t.tno = c.tno
where t.tname = '张旭';

18、查询95033班和95031班全体学生的记录。

select * from student where class in ('95033','95031');

19、查询存在有85分以上成绩的课程Cno。

select cno from score as s1 where
85 < any(select degree from score as s2 where s1.cno = s2.cno)
group by cno;

20、询出“计算机系“教师所教课程的成绩表

## 计算机系的教师的课程号
select cno from course where tno in
(select tno from teacher where depart = '计算机系');

## 查询出计算机系的老师的课程的成绩
select sno,cno,degree from score
where cno in (select cno from course where tno in
(select tno from teacher where depart = '计算机系'));

21、查询所有教师和同学的name、sex和birthday。

union 和 union all 的区别是,union 会自动压缩多个结果集合中的重复结果,而 union all 则将所有的结果全部显示出来,不管是不是重复。union 的作用就类似于上下拼接

select sname as ‘name’,ssex as sex,sbirthday as birthday from student
union
select tname,tsex,tbirthday from teacher;

22、查询所有“女”教师和“女”同学的name、sex和birthday。

select sname as 'name',ssex as sex,sbirthday as birthday from student
where ssex = '女'
union
select tname,tsex,tbirthday from teacher where tsex = '女';

23、查询成绩比该课程平均成绩低的同学的成绩表。

## 每门课程的平均成绩
select avg(degree) from score group by cno;
## 
select * from score as sc1 where
degree < (select avg(degree) from score as sc2 where sc1.cno = sc2.cno);

24、查询所有任课教师的Tname和Depart

select tname,depart from teacher where tno in (select tno from course);

25、查询至少有2名男生的班号。

select distinct class from student as s1
where 2 <= (select count(*) from student as s2 where s1.class = s2.class and s1.ssex = '男');

26、查询Student表中不姓“王”的同学记录。

select * from student where sname not like '王%';

27、查询Student表中每个学生的姓名和年龄。

## select now();
select sname,year(now())-year(sbirthday) from student;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

念犯困

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值