MySQL 查询练习记录

最近在复习mysql,在b站上找了一个感觉还不错的视频,把视频中查询练习相关的内容记录了下来,以便自己日后查阅和复习。

视频连接:https://www.bilibili.com/video/av39807944/?p=1

数据准备

创建数据表

学生表 student:

1
2
3
4
5
6
7
create table student(
sno varchar(20) primary key,
name varchar(10) not null,
sex varchar(10) not null,
birthday datetime,
class varchar(20)
);

教师表 teacher:

1
2
3
4
5
6
7
8
create table teacher(
tno varchar(20) primary key,
name varchar(20) not null,
sex varchar(10) not null,
birthday datetime,
prof varchar(20),
depart varchar(20) not null
);

课程表 course:

1
2
3
4
5
6
create table course(
cno varchar(20) primary key,
name varchar(32) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);

成绩表 score:

1
2
3
4
5
6
7
8
create table score(
sno varchar(20) not null,
cno varchar(20) not null,
score decimal,
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
primary key(sno, cno)
);

添加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- 学生表
insert into student values('108','曾华','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王丽','女','1976-01-23','95033');
insert into student values('101','李军','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陆君','男','1974-06-03','95031');
insert into student values('102','后裔','男','1976-02-20','95033');
insert into student values('104','赵云','男','1975-02-10','95031');
insert into student values('106','王昭君','女','1974-06-03','95031');
-- 教师表
insert into teacher values('804','李成','男','1958-12-02','副教授','计算机系');
insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('825','王小义','女','1972-05-05','助教','计算机系');
insert into teacher values('831','刘冰','女','1977-08-15','猪脚','电子工程系');
-- 课程表
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-166','数字电路','856');
insert into course values('9-888','高等数学','831');
-- 成绩表
insert into score values('103','3-245','86');
insert into score values('105','3-245','92');
insert into score values('109','3-245','75');
insert into score values('103','3-105','58');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','6-166','79');
insert into score values('105','6-166','81');
insert into score values('109','6-166','94');
insert into score values('102','3-105','90');
insert into score values('106','3-105','82');

查询练习题汇总

  1. 查询student表的所有记录
  2. 查询student表中的所有记录的name、sex和class列
  3. 查询教师所有的部门即不重复的depart列
  4. 查询score表中成绩在60到80之间的所有记录
  5. 查询score表中成绩为85,86或88的记录
  6. 查询student表中“95031”班或性别为“女”的同学记录
  7. 以class降序查询student表的所有记录
  8. 以cno升序、degree降序查询score表的所有记录
  9. 查询“95031”班的学生人数
  10. 查询score表中的最高分的学生学号和课程号(子查询或者排序)
  11. 查询每门课的平均成绩
  12. 查询score表中至少有2名学生选修的并以3开头的课程的平均分数
  13. 查询分数大于70,小于90的sno列
  14. 查询所有学生的 sname、cno 和 degree 列
  15. 查询所有学生的sno、cname和degree列
  16. 查询所有学生的sname、cname和degree列
  17. 查询“95031”班学生每门课的平均分
  18. 查询选修“3-105”课程的成绩高于“109”号同学“3-105”课程成绩的所有同学的记录
  19. 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
  20. 查询和学号为108、101的同学同年出生的所有学生的sno、sname和sbirthday列
  21. 查询“张旭”教师任课的学生成绩
  22. 查询选修某课程的同学人数多于5人的教师姓名
  23. 查询“95033”班和“95031”班全体学生的记录
  24. 查询存在有85分以上成绩的课程cno
  25. 查询出“计算机系”教师所教课程的成绩表
  26. 查询“计算机系”和“电子工程系”不同职称的教师的tname和prof
  27. 查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”的同学的cno、sno、和degree,并按degree从高到低次序排序
  28. 查询选修编号为“3-105”课程且成绩高于选修编号为“3-245”的同学的cno、sno、和degree
  29. 查询所有教师和同学的name、sex和birthday
  30. 查询所有“女”教师和“女”同学的name、sex和birthday
  31. 查询成绩比该课程平均成绩低的同学的成绩表
  32. 查询所有任课教师的name和depart
  33. 查询至少有两名男生的班号
  34. 查询student表中不姓“王”的同学记录
  35. 查询student表中每个学生的姓名和年龄
  36. 查询student表中最大和最小的sbirthday日期值
  37. 以班号和年龄从大到小的顺序查询student表中的全部记录
  38. 查询男教师及其所上的课程
  39. 查询最高分同学的sno、cno和degree列
  40. 查询所有和李军同性别的同学的名字
  41. 查询和“李军”同性别并同班的同学的名字
  42. 查询所有选修“计算机导论”课程的“男”同学的成绩表
  43. 假设使用如下命令创建了一个grade表:
1
2
3
4
5
6
7
8
9
10
11
> create table grade(
> low int(3),
> up int(3),
> grade char(1)
> );
> insert into grade values(90,100,"A");
> insert into grade values(80,89,"B");
> insert into grade values(70,79,"C");
> insert into grade values(60,69,"D");
> insert into grade values(50,59,"E");
>

现查询所有同学的sno、cno和grade列。

参考答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1. select * from student;
2. select name,sex,class from student;
3. select distinct depart from teacher;
4. select * from score where degree between 60 and 80;
或 select * from score where degree > 60 and degree < 80;
5. select * from score where degree in (85,86,88);
6. select * from student where class="95031" or sex="女";
7. select * from student order by class desc;
8. select * from score order by cno asc,degree desc;
9. select count(*) from student where class='95031';
10. select sno, cno from score order by degree desc limit 1;
或 select sno,cno from score where degree=(select max(degree) from score);
11. select cno,avg(degree) from score group by cno;
12. select cno,avg(degree) from score group by cno having count(cno)>=2 and cno like '3%';
13. select sno,degree from score where degree > 70 and degree < 90;
14. select name,cno,degree from student,score where student.sno=score.sno;
15. select sno,name,degree from course,score where course.cno=score.cno;
16. select student.name as sname,course.name as cname,score.degree from student,course,score where student.sno=score.sno and course.cno=score.cno;
17. select cno,avg(degree) from score where sno in (select sno from student where class='95031') group by cno;
18. select * from score where degree > (select degree from score where sno='109' and cno='3-105') and cno='3-105';
19. select * from score where degree > (select degree from score where sno='109' and cno='3-105');
20. select sno,name,birthday from student where year(birthday) in (select year(birthday) from student where sno in (101,108));
21. select * from score where cno=(select cno from course where tno=(select tno from teacher where name='张旭'));
22. select name from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(cno)>=5));
23. select * from student where class in ("95033","95031");
24. select cno,degree from score where degree > 85;
25. select * from score where cno in (select cno from course where tno in (select tno from teacher where depart="计算机系"));
26. select name,prof from teacher where depart='电子工程系' and prof not in (select prof from teacher where depart='计算机系')
union
select name,prof from teacher where depart='计算机系' and prof not in (select prof from teacher where depart='电子工程系');
27. select * from score
where cno='3-105'
and degree>any(select degree from score where cno='3-245')
order by degree desc;
28. select * from score
where cno='3-105'
and degree>all(select degree from score where cno='3-245');
29. select name,sex,birthday from teacher
union
select name,sex,birthday from student;
30. select name,sex,birthday from teacher where sex='女'
union
select name,sex,birthday from student where sex='女';
31. select * from score a where degree < (select avg(degree) from score b where a.cno=b.cno);
32. select teacher.name as tname,course.name as cname,teacher.depart from teacher,course where teacher.tno=course.tno;
33. select class from student where sex='男' group by class having count(class)>2;
34. select * from student where name not like "王%";
35. select name,year(now())-year(birthday) as '年龄' from student;
36. select max(birthday) as max_bd,min(birthday) as min_bd from student;
37. select * from student order by class desc,birthday;
38. select * from course where tno in (select tno from teacher where sex='男');
39. select * from score where degree=(select max(degree) from score);
40. select name from student where sex=(select sex from student where name='李军');
41. select name from student
where sex=(select sex from student where name='李军')
and class=(select class from student where name='李军');
42. select * from score
where cno=(select cno from course where name='计算机导论')
and sno in (select sno from student where sex='男');
43. select sno,cno,degree,grade from score,grade where degree between low and up;
 

EOF

 

MySQL 查询练习记录的更多相关文章

  1. mysql查询所有记录,并去掉重复的记录

    select * from tablename group by name;如果是select * from tablename group by name,age;那么查询的是满足name和age都 ...

  2. MYSQL查询重复记录的方法

    select * from hengtu_demandpush a where (a.did,a.mid) in (select did,mid from hengtu_demandpush grou ...

  3. Mysql查询重复记录

    第一步 使用group by 和 having cout 查找重复字段 SELECT t1.`order_book_id` FROM `quant_stock_info` t1 GROUP BY t1 ...

  4. MySQL查询重复出现次数最多的记录

    MySQL查询的方法很多,下面为您介绍的MySQL查询语句用于实现查询重复出现次数最多的记录,对于学习MySQL查询有很好的帮助作用. 在有些应用里面,我们需要查询重复次数最多的一些记录,虽然这是一个 ...

  5. mysql查询在一张表不在另外一张表的记录

    mysql查询在一张表不在另外一张表的记录   问题:    查询一个表(tb1)的字段记录不在另一个表(tb2)中      条件:tb1的字段key的值不在tbl2表中      -------- ...

  6. 【MySQL】过滤后的结果集较大,用LIMIT查询分页记录,查询效率不理想

    > 参考的优秀文章 优化LIMIT分页--<高性能MySQL>(电子工业出版社) > 场景描述 遇到一个场景:查询排序后的结果集较大,我们采用分页显示,每页显示20条记录,但是 ...

  7. mysql 查询一条记录的下一条和上一条记录

    如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from tab ...

  8. mysql 查询随机条记录的sql语句和php计算概率

    最近在网上找了下mysql查询随机的几个sql,我把最终的记录下来. SELECT * FROM uchome_mtag AS a JOIN (SELECT MAX(tagid) AS id FROM ...

  9. mysql 查询每个分组前N条记录

    mysql 查询每个分组前N条记录 假设存在表movie,  有字段 id, part(地区), mcount(观看次数) 现查询每个地区观看次数最多的3部movie, 则表 ###id虽未存在gro ...

随机推荐

  1. 解决ini-parser解析ini文件中文乱码问题

    rickyah/ini-parser 是一个.net 平台解析ini文件的库,当ini文件中含有中文字符时会乱码. 解决:将文件通过Editplus 等文本编辑工具保存为 utf-8 + bom 格式 ...

  2. Struts2 自定义输入校验 第五弹

    Struts2的校验框架有两种:一种是validate方法,另一种是有效的xml文件. Action中自定义方法的输入校验,对于通过action的method属性所指定的自定义方法myExecute, ...

  3. win32com操作word(2):常用用法

    一.对象的位置关系: 1.Range属性位于(部分): Selection__Section__Cell__Paragraph__Table__Bookmark__Comment__Row__List ...

  4. boost库安装和使用

    1. 下载最新的boost库:http://www.boost.org/本文使用的是boost_1_66_0.tar.gz, 2. Boost库安装步骤: > 解压下载文件,例如下载文件在~/D ...

  5. Tomcat的安装及使用

    下面是我搭建Tomcat的过程,记录一下 下载地址:http://tomcat.apache.org/  我下载的是8.5.30版本 安装 下载完成后解压到D盘 (配置变量的的教程网上大把随便搜) 1 ...

  6. 同时安装Python2与Python3,安装第三方包,老是报错

    同时安装Python2与Python3,安装第三方包,老是报错提示Fatal error in launcher: Unable to create process using '"',那可 ...

  7. AngularJs(Part 3)--注册服务

    有以下5中方法注册一个AngularJS可以识别的Service value和constant是两个极其简单的方法,只有少数情况下会使用. service已经开始复杂了起来.而factory是我认为既 ...

  8. Newtonsoft.Json null值不序列化

    如果对当前序列化的实体全部生效的话使用如下: var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandl ...

  9. 《SpringBoot揭秘 快速构建微服务体系》读后感(一)

    SpringIOC IOC有两种方式:一种是DI,另一种是DL,即Dependency Lookup(依赖查找).前者是当前软件实体被动接受其依赖的其他组件被IoC容器注入,而后者则是当前软件实体主动 ...

  10. ACM-ICPC2018北京网络赛 80 Days(双端队列+尺取)

    题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...