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. c语言学习的第11天 指针

    #include<stdio.h> int main(void) { int * p; int i=3; int j; p=&i; j=*p; printf("i=%d, ...

  2. awk添加文本到指定内容后一行

    一下操作会输入到文件末尾,,测试不全面,还不如使用echo解决呢... 1. 要添加的文本内容如下: 文件名:code_list html</>!@$%^^ fdsafds <> ...

  3. ios图片瀑布流代码

    ios瀑布流,实现简单的瀑布流视图布局,可以显示网络图片,下拉刷新,上拉加载更多. 下载:http://www.huiyi8.com/sc/9087.html

  4. openfire开发环境(3.9.1)

    1.解压源码 2.把build/eclipse中的文件cp到源码跟目录,并修改文件名,前面增加"."号,变成eclipse工程. 3.导入eclipse, 把build/lib/, ...

  5. listen and translation exercise 53

    It was hard work and there weren't any interesting things for him. You should be an expert with comp ...

  6. install docker

    摘要: 我的环境是:CentOS-7-x86_64-Minimal-1511.iso , 也可参考docker官网文档,来安装, url : https://docs.docker.com/engin ...

  7. linux命令学习笔记(33):df 命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了 多少空间,目前还剩下多少空间等信息. .命令格式: df [选项] [文件] .命令 ...

  8. oracle 11g 常用命令

    sqlplus system/123@ORCL; 查看oracle字符集: select * from nls_database_parameters where parameter ='NLS_CH ...

  9. 闪回之 Flashback Query (dml表、过程、函数、包等)、Flashback version Query

    Flashback Query 背景:Flashback 是 ORACLE 自 9i 就开始提供的一项特性,在 9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数 ...

  10. java面向对象的三大特性

    1.面向对象的三大特性 继承.封装.多态 什么是继承? ①继承是面向对象程序设计能够提高软件开发效率的重要原因之一. ②继承是具有传递性的,就像现实中孙子不仅长得像爸爸而且还像他爷爷. ③继承来的属性 ...