题目:设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

student(学生表)

属性名 可否为空 数据类型 含义
sno varchar(20) 学号(主键)
sname varchar(20) 学生姓名
ssex varchar(20) 学生性别
sbirth datetime 学生出生年月日
class varchar(20) 学生所在班级

student表中的数据

 //1.添加学生信息insert into student values ('101','曾华','男','1977-09-01','95033');insert into student values ('102','王丽','女','1975-10-02','95032');insert into student values ('103','匡明','男','1976-01-23','95033');insert into student values ('108','李军','男','1976-02-20','95030');insert into student values ('105','张芳','女','1975-02-10','95031');insert into student values ('106','李楠','女','1974-06-03','95032');insert into student values ('107','程君','女','1977-08-01','95033');

Teacher(教师表)

属性名 数据类型 含义 可否为空
Tno varchar(20) 教师编号(主键)
Tname varchar(20) 教师名称
Tsex varchar(20) 教师性别
Tbirth datetime 出生年月日
Prof varchar(20) 职称
Depart varchar(20) 所在部门
 //2.添加教师表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-14','助教','电子工程系');

Course(课程表)

属性名 数据类型 含义 可否为空
cno varchar(20) 课程号(主键)
cname varchar(20) 课程名称
tno varchar(20) 教师编号(外键)
 //3.添加课程表insert into course values ('3-105','计算机导论','825');insert into course values ('3-245','操作系统','804');insert into course values ('6-616','数字电路','856');insert into course values ('9-888','高等数学','831');

Score(成绩表 )

属性名 数据类型 含义 可否为空
sno varchar(20) 学号(外键)
cno varchar(20) 课程号(外键)
degree decimal(4,1) 成绩
 //4.添加成绩表insert into score values ('103','3-245','86');insert into score values ('105','3-245','75');insert into score values ('108','3-245','68');insert into score values ('102','3-245','90');insert into score values ('101','3-245','87');insert into score values ('103','3-105','92');insert into score values ('105','3-105','88');insert into score values ('108','3-105','76');insert into score values ('103','9-888','64');insert into score values ('105','9-888','91');insert into score values ('108','9-888','78');insert into score values ('103','6-616','85');insert into score values ('105','6-616','79');insert into score values ('108','6-616','81');insert into score values ('101','6-616','78');insert into score values ('102','6-616','92');

查询问题:

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

 select sname,ssex,class from student;

2、 查询教师所有的单位, 即不重复的Depart列。

 select  distinct depart from teacher;//查找不重复的depart列,就是对这个列去重,使用distinct

3、 查询Student表的所有记录。

 select * from student;

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

 //方法1:使用between。。and。。select * from score where degree between 60 and 80;//方法2:使用‘>’,‘<’比较select * from score where degree > 60 and degree < 80;

5、 查询Score表中成绩为85,86或88的所有记录。

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

6、 查询Student表中“95031”班或性别为“女”的同学记录。

 select * from student where class='95031' or ssex = '女';

7、 以Class降序查询Student表的所有记录。

 select * from studentorder by class desc;

8、 以Cno升序、Degree降序查询Score表的所有记录。

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

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

 select count(*) as "95031班的总人数" from student where class='95031';

10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

 //子查询:也就是嵌套查询.(先查出最高的成绩)select sno,cno from scorewhere degree = (select max(degree) from score);
 //排序进行查询select sno,cno,degree from score order by degree desc limit 1;

11、 查询每门课的平均成绩。

 select cno,avg(degree) as '平均成绩' from scoregroup by cno;

12、查询Score表中至少有4名学生选修的,并且以3开头的课程的平均分数。【where】

 //思路//第一步:先查出那些课程cno的选修人数大于4人的学号select cno from scoregroup by cnohaving count(cno)>4;//第二步:加上where 筛选出以3 开头的课程select cno from scorewhere cno like '3%'group by cnohaving count(cno)>4;//第三步:查询条件中 加上平均分select cno,avg(degree) As '平均分' from scorewhere cno like '3%'group by cnohaving count(cno)>4;select cno,avg(degree) from scoregroup by cnohaving count(*)>4 and cno like '3%';

13、查询分数大于70,小于90的Sno列。

 //方法1:查询出来的值,不包含临界值 70,90。select sno,degree from scorewhere degree > 70 and degree < 90;//方法2:使用between。and。查询出来的数据包含临界值 70,90。select sno,degree from scorewhere degree between 70 and 90;

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

 //方法1:使用where select sname,cno,degree from student,scorewhere student.sno = score.sno;//方法2:使用join...on../ inner join ... onselect sname,cno,degree from student inner join/join score onwhere student.sno = score.sno;

15、查询所有学生的Sno、Cname和Degree列。

 //方法1:使用whereselect sno,cname,degree from score,coursewhere score.cno = course.cno;//方法2:使用 inner join on/ join onselect sno,cname,degree from score inner join course on score.cno = course.cno;select cname,sno,degree from scorejoin course on score.cno = course.cno;

16、查询所有学生的Sname、Cname和Degree列.

 //方法1:select sname,cname,degree from student,score,coursewhere student.sno = score.sno and course.cno = score.cno;//方法2:select sname,cname,degree from studentinner join score on student.sno = score.snoinner join course on course.cno = score.cno;

17、 查询“95033”班学生的平均分。

 //第一步:先查询95033班的学生信息select * from studentwhere class = '95033';//第二步:查询95033班 学生的没门课程的成绩select sname,class,cno,degree from student,scorewhere student.sno = score.sno and class = '95033';

 //第三步:按照各个学科的学号进行排序,select cno,avg(degree) from student,scorewhere student.sno = score.sno and class = '95033'group by cno;

[★★]18、 假设使用如下命令建立了一个grade表:

        create table grade(low int(3),upp int(3),rank_1 char(2));
        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(0,59,'E');

现查询所有同学的Sno、Cno和rank_1 列。

 //第一步:查询成绩表scoreselect * from score;//第二步:查询grade表select * from grade;

 //第三步:怎么将 成绩表score 中的degree 与 grade中的rank——1进行意义对应select sno,cno,rank_1 from score,gradewhere degree between low and upp;    /*再grade表中,可以知道这是一个对成绩划分等级的表数据,想要将score成绩表中 成绩对应到grade表中的各个等级,可以使用 between  and  将 等级表中的各个级别 对应到成绩中*/

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

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

20、查询score中选学多门课程的同学中分数为 非最高分 成绩的记录。

 //第一步:筛选除 选修了 1门以上选修课程的同学  学号 snoselect sno from scoregroup by snohaving count(cno)>1;//查询非最高 成绩的记录select sno from scoregroup by snohaving count(cno)>1 and degree not in (select max(degree) from score where count(cno)>1);

21、查询成绩高于学号为“108”、课程号为“3-105”的成绩的所有记录。

 //第一步:先查出学号108 课程号为3-105 同学 所有记录select * from score where sno = '108' and cno = '3-105';

 //第二步:第一步的值返回学生的成绩,作为子查询。然后查询课程号为3-105课程select * from scorewhere cno = '3-105' and degree > (select degree from score where sno = '108' and cno = '3-105');

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

 //第一步:先筛选出学号108 101 同学的出生年分,使用year函数select year(sbirth) from studentwhere sno = '108' or sno = '101';
 //第二步:查找1977 1976年出生的学生信息select sno,sname,sbirth from studentwhere year(sbirth) in(select year(sbirth) from studentwhere sno = '108' or sno = '101');

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

 //第一步:连接teacher表和course表,查出张旭老师任课的课程号select cno from teacher inner join course on teacher.tno = course.c_tnowhere tname = '张旭';
 //第二步:查出选修了 6-616 课程号的学生信息select * from scorewhere cno = (select cno from teacher inner join course on teacher.tno = course.c_tnowhere tname = '张旭');

24、查询选修某课程的同学人数多于4人的教师姓名。

 //第一步:先查出选修的人数 大于5人的  课程号select cno from scoregroup by cnohaving count(sno)>4;
 //第二步:将teacher表和course表进行内连接,筛选条件为 选修课程人数大于4人的 课程号select * from teacher inner join course on teacher.tno = course.c_tnowhere cno in (select cno from scoregroup by cnohaving count(sno)>4);

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

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

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

 select sno,cno from scorewhere degree > 85;

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

 //第一步:查出计算机系 教师的教师编号select tno from teacherwhere depart = "计算机系";
 //第二步:将成绩表 score 与course表进行lianjieselect score.cno,degree from scoreinner join courseon score.cno = course.cnowhere course.c_tno in (select tno from teacherwhere depart = "计算机系");

方法2:

 //第一步:先连接 teacher表和 course表,查出符合条件的 cnoselect cno from teacher inner join courseon teacher.tno = course.c_tnowhere depart = '计算机系';
 //第二步:上一步查出的 cno 与score成绩表 进行查询select cno,degree from scorewhere cno in (select cno from teacher inner join courseon teacher.tno = course.c_tnowhere depart = '计算机系');

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

 //第一步:查出计算机系的教师职称select prof from teacher where depart = "计算机系";//第二步:查出电子工程系 教师的职称select prof from teacher where depart = "电子工程系";//第三步:使用in 查出计算机系与电子工程系 相同职称 的教师信息select tname,prof from teacherwhere depart = "计算机系" and prof in (select prof from teacher where depart = "电子工程系");/*子查询中  查出了电子工程系教师的职称有:助教 讲师外面的查询中 查出计算机系教师的职称 并且查出来的职称在电子工程系  也有 将他的名字和职称都显示出来*///答案:使用 not in 查出 计算机系与电子工程系 中 不同职称的教师select tname,prof from teacherwhere depart = "计算机系" and prof not in (select prof from teacherwhere depart = "电子工程系");

[★★★]29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

至少?大于其中至少一个,any

 //第一步:查出3-105课程的成绩select sno,degree from scorewhere cno = '3-105';
        //第二步:查出3-245课程的成绩select sno,degree from scorewhere cno = '3-245';
 //第三步:题目要求,3-105的成绩 至少 高于3-245中的最低分select sno,degree from scorewhere cno = '3-105' and degree > any(select degree from scorewhere cno= '3-245');
 //答案:select sno,cno,degree from scorewhere cno = '3-105' and degree > any(select degree from scorewhere cno= '3-245')order by degree desc;

30、查询选修编号为“3-105” 且 成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

且?就是大于任何一个,all表示所有的意思

思路:3-105中学生的成绩需要 大于 3-245课程的最大分数

 select cno,sno,degree from scorewhere cno = '3-105' and degree > all(select degree from scorewhere cno = '3-245');

31、 查询所有教师和同学的name、sex和birthday.

union就是取交集

     select sname as name,ssex as sex,sbirth as birth from studentunionselect tname,tsex,tbirth from teacher;

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

     select sname as name,ssex as sex,sbirth as birth from studentunionselect tname,tsex,tbirth from teacherwhere tsex = '女';

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

 select tname,depart from teacherwhere tno in (select c_tno from course);/*使用 in 查询有哪些教师 任课,需要将teacher表中tno与course表中的c_tno进行匹配,选出 交集*//*使用 not in 可以筛选出那些没有上课的老师*/

35 、 查询所有未讲课的教师的Tname和Depart.

 select tname,depart from teacherwhere tno not in (select c_tno from course);

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

 //先按班号进行分组,然后筛选出 男生人数大于 2的select class from studentwhere ssex = '男'group by classhaving count(ssex)>2 ;

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

 select * from studentwhere sname not like '王%';

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

年龄:当前年份-出生年份

year(now())-year(sbirth)

查看当前年份:year(now())

 select sname,year(now())-year(sbirth) from student;

39、查询Student表中最大和最小的Sbirthday日期值。

 select max(sbirth),min(sbirth) from student;

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

 select sname,class, year(now())-year(sbirth) from studentorder by class desc,year(now())-year(sbirth) desc;

41、查询“男”教师及其所上的课程。

 //方法1:使用内连接select cno from teacherinner join course on teacher.tno = course.c_tnowhere tsex = '男';
  //方法2:使用 in关键字select cno,cname from coursewhere c_tno in (select tno from teacherwhere tsex = '男');

[★★]42、查询最高分同学的Sno、Cno和Degree列。

方法1使用 limit 获取最高分:使用 order by 先排序,然后取出最高分 降序排序取第一个值【不推荐使用】

        select sno,cno,degree from scoreorder by degree desc limit 1;

使用 limit 获取最高分,如果同一个最高分有多个值,那么limit 就不推荐使用

方法2:使用 max()求取最大值的函数【不推荐使用】

     select sno,cno,max(degree) from score;

使用max()函数进行查询,只能取一个值,但是 最高分92,有两个值

方法3:使用子查询查出最高分【推荐使用】

     select sno,cno,degree from scorewhere degree in (select max(degree) from score);

43、查询和“李军”同性别的所有同学的Sname.

 select sname from studentwhere ssex =(select ssex from student where sname = "李军");

44、查询和“李军”同性别并同班的同学Sname.

 select sname from studentwhere ssex =(select ssex from student where sname = "李军")and class in (select class from student where sname = "李军") ;

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

 //第一步:先查出计算机导论的课程编号select cno from course where cname = "计算机导论";//第二步:查出选修了 3-105课程号的学生select score.sno,cno,degree from score inner join studenton student.sno = score.snowhere ssex = '男' andcno = (select cno from course where cname = "计算机导论");
 //将score表与student表 连起来select * from score inner join studenton student.sno = score.snowhere ssex = '男' andcno = (select cno from coursewhere cname = "计算机导论");

MySQL-基础练习题1相关推荐

  1. 燕十八--Mysql基础练习题

    mysql复习 一:复习前的准备 1:确认你已安装wamp 2:确认你已安装ecshop,并且ecshop的数据库名为shop 二 基础知识: 1.数据库的连接 mysql -u -p -h -u 用 ...

  2. MySQL基础 -- 练习题

    一.已有表文件 STOCK (股票代码 C(6),买入价 N(10.2),现价 N(10.2),持有数量 N(10)),按照如下要求写出SQL语句: 1.查询 股票代码 为"000625&q ...

  3. MySQL基础 练习题2

    1. 查询 students 表中的所有记录的 sname.ssex 和 class 列. 2. 查询教师所有的单位即不重复的 depart 列. 3. 查询 students 表的所有记录. 4. ...

  4. MySQL基础练习题

    1) 查询出 goods 表中所有字段 2) 查询出 goods 表中生产日期在一年前的商品 3) 查询出 goods 表中商品名称中带"洗"字的商品 4) 查询出 goods 表 ...

  5. MySQL基础练习题及答案(表的简单查询)-02

    一.查询"李"姓老师的数量 思路:在老师(teacher)表中查找老师名字并筛选出以'李'开头的,再使用count()函数求出个数,最后重命名搜索出来的这个列名为姓"李& ...

  6. 六、MySQL 数据库练习题1(包含前5章练习题目及答案)

    文章目录 一.数据库概述练习题 二.MySQL 环境搭建练习题 三.查询练习 MySQL 数据库练习题(包含前5章所有知识点及答案) 前置知识: 一.数据库开发与实战专栏导学及数据库基础概念入门 二. ...

  7. MySQL数据库入门学习教程(mysql基础+高级)

    今天这篇文章将详细列出Mysql的学习流程,这是学习mysql数据库前你要了解的~~~ 大部分的小伙伴自己在网上找mysql资料.还有数据库的视频教程,但是都过于碎片化,没有体系,导致大家不知道如何系 ...

  8. JavaScript基础练习题(四)

    JavaScript基础练习题(四) 一.单选题 1.同步和异步执行分别表示什么含义 A 同步是按顺序依次执行:异步是同时分开一起执行 B 同步是同时分开一起执行:异步是按顺序依次执行 C 同步是按一 ...

  9. Mysql基础进阶运维(确定不来看看?)

    提示:mysql基础,进阶,运维(持续更新,更新速度与本人学习进度一致,加入自己的认知和理解,以下操作主要针对8.0后的) 文章目录 一.Mysql基础 1 Mysql概述 1.1 数据库相关概念 1 ...

  10. MySQL基础-学习笔记

    MySQL基础 一.DQL语言的学习 1.基础查询 /* 语法: select 查询列表 from 表名特点: 1-查询的结果集是一个虚拟表 2-select类似于System.out.printIn ...

最新文章

  1. 图论 ---- D. Multiples and Power Differences (全局lcm + 矩阵二分图)
  2. 弄了个调试呼叫中心用的小机器
  3. Matlab中函数使用
  4. corosync+pacemaker+nfs提供mysql高可用
  5. 使用Flex Bison 和LLVM编写自己的编译器[zz]
  6. 【Hibernate】getHibernateTemplate.find()和session.createQuery()方法总结
  7. Android监听事件
  8. Linux搜索无线网络命令,Linux操作系统的无线网络命令
  9. Java中的关键字@Override解释
  10. python-socket模块介绍
  11. 经典的EDA设计软件Protel99SE
  12. iOS 在CollectionView上做展开收起动画
  13. Tableau——通过数据透视表实现多表关联
  14. ?username=王二麻子age=18转换成对象?
  15. OSChina 周二乱弹 —— 你一辈子都不可能跟她这么亲近
  16. linux 查看mmc分区_Linux MMC介绍
  17. 视觉SLAM十四讲读书笔记(2)P10-P27
  18. 计算机系统i3和i6区别,英特尔内核迭代,有i3 i5 i7,没有i4 i6吗?
  19. 计算2000年1月1日到2008年1月1日 相距多少天。
  20. 网狐棋牌 SQL Server 数据库配置

热门文章

  1. 计算机操作系统经典进程同步问题
  2. 使用163邮箱发送邮件报错(554, b'DT:SPM 163 smtp3,G9xpCgCHi5RJOFVemMZ4Dw--.348S3 1582643274,please see http://ma
  3. 后科技时代—赛博朋克2077
  4. 【DKN】(五)attention.py
  5. 基于MATLAB的人脸识别系统
  6. Python:import与from import的理解
  7. springboot项目中的 Request method 'POST' not supported问题
  8. 学渣的刷题之旅 leetcode刷题 28. 实现 strStr()
  9. Andriod微信小程序自动化测试
  10. 双11大队长霜波:从手忙脚乱到胸有成竹,我们如何走过这十年?