补充:MySQL经典45道题型
一、 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。
四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。
表1-1数据库的表结构
表(一)Student (学生use)
|
属性名 |
数据类型 |
可否为空 |
含 义 |
|
Sno |
varchar (20) |
否 |
学号(主码) |
|
Sname |
varchar (20) |
否 |
学生姓名 |
|
Ssex |
varchar (20) |
否 |
学生性别 |
|
Sbirthday |
Datetime |
可 |
学生出生年月 |
|
Class |
varchar (20) |
可 |
学生所在班级 |
表(二)Course(课程表)
|
属性名 |
数据类型 |
可否为空 |
含 义 |
|
Cno |
varchar (20) |
否 |
课程号(主码) |
|
Cname |
varchar (20) |
否 |
课程名称 |
|
Tno |
varchar (20) |
否 |
教工编号 |
表(三)Score(成绩表)
|
属性名 |
数据类型 |
可否为空 |
含 义 |
|
Id |
Int |
主键 自增 |
|
|
Sno |
varchar (20) |
否 |
学号 |
|
Cno |
varchar (20) |
否 |
课程号 |
|
Degree |
Decimal(4,1) |
可 |
成绩 |
|
//主码:Sno+ Cno |
|||
表(四)Teacher(教师表)
|
属性名 |
数据类型 |
可否为空 |
含 义 |
|
Tno |
varchar (20) |
否 |
教工编号(主码) |
|
Tname |
varchar (20) |
否 |
教工姓名 |
|
Tsex |
varchar (20) |
否 |
教工性别 |
|
Tbirthday |
datetime |
可 |
教工出生年月 |
|
Prof |
varchar (20) |
可 |
职称 |
|
Depart |
varchar (20) |
否 |
教工所在部门 |
表1-2数据库中的数据
表(一)Student
|
Sno |
Sname |
Ssex |
Sbirthday |
class |
|
108 |
曾华 |
男 |
1977-09-01 |
95033 |
|
105 |
匡明 |
男 |
1975-10-02 |
95031 |
|
107 |
王丽 |
女 |
1976-01-23 |
95033 |
|
101 |
李军 |
男 |
1976-02-20 |
95033 |
|
109 |
王芳 |
女 |
1975-02-10 |
95031 |
|
103 |
陆君 |
男 |
1974-06-03 |
95031 |
表(二)Course
|
Cno |
Cname |
Tno |
|
3-105 |
计算机导论 |
825 |
|
3-245 |
操作系统 |
804 |
|
6-166 |
数字电路 |
856 |
|
9-888 |
高等数学 |
831 |
表(三)Score
|
Sno |
Cno |
Degree |
|
103 |
3-245 |
86 |
|
105 |
3-245 |
75 |
|
109 |
3-245 |
68 |
|
103 |
3-105 |
92 |
|
105 |
3-105 |
88 |
|
109 |
3-105 |
76 |
|
101 |
3-105 |
64 |
|
107 |
3-105 |
91 |
|
108 |
3-105 |
78 |
|
101 |
6-166 |
85 |
|
107 |
6-166 |
79 |
|
108 |
6-166 |
81 |
表(四)Teacher
|
Tno |
Tname |
Tsex |
Tbirthday |
Prof |
Depart |
|
804 |
李诚 |
男 |
1958-12-02 |
副教授 |
计算机系 |
|
856 |
张旭 |
男 |
1969-03-12 |
讲师 |
电子工程系 |
|
825 |
王萍 |
女 |
1972-05-05 |
助教 |
计算机系 |
|
831 |
刘冰 |
女 |
1977-08-14 |
助教 |
电子工程系 |
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
2、Select sname,ssex,class from student;
3、 查询教师所有的单位即不重复的Depart列。
4、Select distinct depart from teacher;
5、 查询Student表的所有记录。
6、Select * from student;
7、 查询Score表中成绩在60到80之间的所有记录。
8、Select * from score where degree between 60 and 80 ;
9、 查询Score表中成绩为85,86或88的记录。
10、select * from score where degree in(85,86,88);
11、 查询Student表中“95031”班或性别为“女”的同学记录。
12、select * from student where class=”95031” or ssex=”女”;
13、 以Class降序查询Student表的所有记录。
14、Select * from student order by class desc;
15、 以Cno升序、Degree降序查询Score表的所有记录。
16、Select * from score order by cno, degree desc;
17、 查询“95031”班的学生人数。
18、Select count(*) from student where class=”95031”;
19、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
20、 select max(cno) from score where sno=(select max(sno) from score where degree=(Select max(degree) from score));
21、 查询每门课的平均成绩。
22、Select cno,avg(degree) as avg from score group by cno;
23、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
24、 select cno,avg(degree) from score group by cno having count(*) > 5;
25、查询分数大于70,小于90的Sno列。
26、Select sno from score where degree > 70 and degree < 90;
27、查询所有学生的Sname、Cno和Degree列。
28、Select sname,cno,degree from student join score on student.sno=score.sno;
29、查询所有学生的Sno、Cname和Degree列。
30、 Select sno,cname,degree from score join course on score.cno=score.cno;
31、查询所有学生的Sname、Cname和Degree列。
32、 Select sname,cname,degree from student join score on student.sno=score.sno join course on course.cno=score.cno;
33、查询“95033”班学生的平均分。
34、 select class,avg(degree) from student join score on student.sno=score.sno where class="95033";
35、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
36、 Select * from student where sno in( Select sno from score where cno="3-105" and degree > 76);
37、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
38、select
39、 score.sno,score.cno,score.degree
40、from
41、 score
42、join
43、 (select cno,max(degree) as d from score group by cno having count(*) > 1) as a
44、on
45、 score.cno = a.cno and score.degree <> a.d;
46、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
47、Select * from score where sno=”109” and cno=”3-105”;
48、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
49、select Sno,Sname,Sbirthday from student where year(student.Sbirthday)=(select year(Sbirthday) from student where Sno='107');
50、查询考计算机导论的学生成绩
51、select degree from score where cno=(select cno from course where cname="计算机导论");
52、教高等数学的老师是哪个系的
53、select depart from teacher where tno=(select tno from course where cname="高等数学");
54、查询选修某课程的同学人数多于5人的教师姓名。
55、找到课程号 选修人数大于五 根据课程号 找老师编号 根据老师编号找到老师姓名
56、select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(*) >5));
57、查询95033班和95031班全体学生的记录。
58、select * from student where class in ('95033', '95031');
59、查询成绩表中存在有85分以上成绩的课程Cno.
60、select distinct cno from score where degree > 85;
61、查询出“计算机系“教师所教课程的成绩表。
62、select * from score where cno in (select cno from course where tno in (select tno from teacher where depart = '计算机系'));
66、根据3-105查出信息 并且 成绩还有要求
67、select cno,min(degree) from score where cno="3-105" and degree>(select min(degree) from score where cno="3-105");
68、查询所有教师和同学的name、sex和birthday.
69、select sname,ssex,sbirthday from student
70、-> union
71、-> select tname,tsex,tbirthday from teacher;
72、查询所有“女”教师和“女”同学的name、sex和birthday.
73、select sname,ssex,sbirthday from student where ssex="女"
74、 -> union
75、 -> select tname,tsex,tbirthday from teacher where tsex="女";
76、查询所有成绩比该课程平均成绩低的同学的成绩表。
77、select Sno,Cno,Degree from Score a where a.Degree<(select AVG(Degree) from Score b where a.Cno=b.Cno)
78、查询所有任课教师的Tname和Depart.
79、select Tname,Depart from Teacher where Tname in (select distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno)
select Tname,Depart from Teacher where tno in (select tno from course where Cno in (select distinct Cno from Score))
80、查询所有未讲课的教师的Tname和Depart.
81、select Tname,Depart from Teacher where Tname not in (select distinct Tname from Teacher,Course,Score where Teacher.Tno=Course.Tno and Course.Cno=Score.Cno)
82、查询至少有2名男生的班号。
83、Select * from student group by ssex,class having count(*) >1;
84、查询Student表中不姓“王”的同学记录。
85、select * from student where Sname not like ('王%')
86、查询Student表中每个学生的姓名和年龄。
87、Student year(now())-year(birthday) from student;
88、查询Student表中最大和最小的Sbirthday日期值。
89、select MAX(Sbirthday) as 最大,MIN(Sbirthday) as 最小 from student
90、以班号和年龄从大到小的顺序查询Student表中的全部记录。
91、select * from student order by Class desc,Sbirthday asc
92、查询“男”教师及其所上的课程。
93、select Tname,Cname from Teacher,Course where Tsex='男' and Teacher.Tno=Course.Tno
94、查询最高分同学的Sno、Cno和Degree列。
95、elect Sno,Cno,Degree from Score where degree=(select MAX(Degree)from Score)
select top 1* from Score order by Degree desc
96、查询和“李军”同性别的所有同学的Sname.
97、select Sname from student where Ssex=(select Ssex from student where Sname='李军') and Sname not in ('李军')
98、查询和“李军”同性别并同班的同学Sname.
99、select Sname from student where Ssex=(select Ssex from student where Sname='李军') and Sname not in ('李军') and Class=(select Class from student where Sname='李军')
100、查询所有选修“计算机导论”课程的“男”同学的成绩表。
101、select Sno,Degree from Score where Sno in (select Sno from student where Ssex='男') and Cno in (select Cno from Course where Cname='计算机导论')
补充:MySQL经典45道题型的更多相关文章
- MySQL查询 45道练习题
SQL查询45道练习题 1.查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from student2.查询教师所有的单位即不重复 ...
- MySQL语句45道练习题及答案
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- MYSQL数据库45道练习题
--第一题查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from Student;--第二题查询教师所有的单位即不重复的Depa ...
- 学生选课数据库SQL语句45道练习题整理及mysql常用函数(20161019)
学生选课数据库SQL语句45道练习题: 一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四 ...
- ASP.NET 经典60道面试题
转:http://bbs.chinaunix.net/thread-4065577-1-1.html ASP.NET 经典60道面试题 1. 简述 private. protected. public ...
- C语言超级经典400道题目
C语言超级经典400道题目 1.C语言程序的基本单位是____ A) 程序行 B) 语句 C) 函数 D) 字符.C.1 2.C语言程序的三种基本结构是____构A.顺序结构,选择结构,循环结 B.递 ...
- 数据库MySQL经典面试题之SQL语句
数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...
- Mysql实战45讲 04讲深入浅出索引(上)读书笔记 极客时间
极客时间 Mysql实战45讲 04讲深入浅出索引 极客时间(上)读书笔记 笔记体悟 1.索引的作用:提高数据查询效率2.常见索引模型:哈希表.有序数组.搜索树3.哈希表:键 - 值(key - v ...
- MySQL基础普及《MySQL管理之道:性能调优、高可用与监控》
最近工作的内容涉及MySQL运维内容,陆陆续续读了几本相关的书,其中一本是<MySQL管理之道:性能调优.高可用与监控>. 内容涵盖性能调优(包括sql优化等).备份.高可用,以及读写分离 ...
随机推荐
- 时间>金钱
时间>金钱! 如果有机会,用你的金钱去换取别人的成功经验,一定要抓住一切机会向顶尖人士学习. 仔细选择你接触的对象,因为这会节省你很多时间. 假设与一个成功者在一起,他花了10年时间成功,你跟1 ...
- [转] 阿里研究员谷朴:API 设计最佳实践的思考
API是软件系统的核心,而软件系统的复杂度Complexity是大规模软件系统能否成功最重要的因素.但复杂度Complexity并非某一个单独的问题能完全败坏的,而是在系统设计尤其是API设计层面很多 ...
- 【C++】C++中assert和ENDEGU预处理语句
assert 断言语句是C++中的一种预处理宏语句,它能在程序运行时根据否定条件中断程序. C++中的assert()函数可以实现断言功能,在使用assert函数之前应该先引入<cassert& ...
- Windows IIS 服务器配置HTTPS启用TLS协议。
好消息, 程序员专用早餐机.和掌柜说 ideaam,可以节省20元. 点击链接 或復·制这段描述¥k3MbbVKccMU¥后到淘♂寳♀ Windows IIS 服务器配置HTTPS启用TLS协议. ...
- select 与 time.After 配合使用的问题
今天在工作中发现了一个有趣的现象. 在一个select中设定了两个定时器,本来预计哪个定时器到达就运行相应指令的,但是发现最终只有时间最短的定时器一直得到执行,其它定时器完全没有得到执行. packa ...
- 【原创 深度学习与TensorFlow 动手实践系列 - 2】第二课:传统神经网络
第二课 传统神经网络 <深度学习>整体结构: 线性回归 -> 神经网络 -> 卷积神经网络(CNN)-> 循环神经网络(RNN)- LSTM 目标分类(人脸识别,物品识别 ...
- 解决Eclipse中“诡异”的错误:找不到或无法加载主类
记录下来遇到的(问题,解决方法),是更有效的解决问题的方式.(原谅我领悟的太晚与懒,从此用更有意义的方法,做一个更有意义的人) 因为遇到了多次,参考同一个方法,原文连接:https://blog.cs ...
- VBA二次学习笔记(1)——文件操作
说明(2018-9-1 11:20:46): 1. 上班三个月了,累的一逼,真的是钱少事多离家远,每天早上六点起,晚上八点回.哎,少壮不努力啊! 2. 三个月没写博客了,上一篇已经是5.29的了,真的 ...
- 快速开发项目,用到的工具:UI 设置利器 sketch
需求设计: axaure8.0 tool: teambition/石墨.幕布. 接口管理tool(后端开发接口,pc,m,app使用) https://www.eolinker.com/#/ ui 设 ...
- 如何知道局域网内哪些ip被占用
诶,有时候真是略捉急,开始的时候估摸了一个网段,试了3个都没有通,觉得这个一个个试验的方法简直捉急到家了.下面就为大家分享3种简单地如何查看ip地址是否被占用的方法. 1.ping windows键+ ...