mysql经典查询练习

转载 https://blog.csdn.net/qq_23994787/article/details/77161741

一、设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。用SQL语句创建四个表并完成相关题目。

表(一)Student (学生表) 

属性名

数据类型

可否为空

含 义

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(成绩表)

属性名

数据类型

可否为空

含 义

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
  2. create table student
  3. (
  4. sno varchar( 20) not null primary key,
  5. sname varchar( 20) not null,
  6. ssex varchar( 20) not null,
  7. sbirthday datetime,
  8. class varchar( 20)
  9. );
  10. #建立教师表
  11. create table teacher
  12. (
  13. tno varchar( 20) not null primary key,
  14. tname varchar( 20) not null,
  15. tsex varchar( 20) not null,
  16. tbirthday datetime,
  17. prof varchar( 20),
  18. depart varchar( 20) not null
  19. );
  20. #建立课程表course
  21. create table course
  22. (
  23. cno varchar( 20) not null primary key,
  24. cname varchar( 20) not null,
  25. tno varchar( 20) not null,
  26. foreign key(tno) references teacher(tno)
  27. );
  28. #建立成绩表
  29. create table score
  30. (
  31. sno varchar( 20) not null primary key,
  32. foreign key(sno) references student(sno),
  33. cno varchar( 20) not null,
  34. foreign key(cno) references course(cno),
  35. degree decimal
  36. );
  37. #添加学生信息
  38. insert into student values( '108', '曾华', '男', '1977-09-01', '95033');
  39. insert into student values( '105', '匡明', '男', '1975-10-02', '95031');
  40. insert into student values( '107', '王丽', '女', '1976-01-23', '95033');
  41. insert into student values( '101', '李军', '男', '1976-02-20', '95033');
  42. insert into student values( '109', '王芳', '女', '1975-02-10', '95031');
  43. insert into student values( '103', '陆君', '男', '1974-06-03', '95031');
  44. #添加教师表
  45. insert into teacher values( '804', '李诚', '男', '1958-12-02', '副教授', '计算机系');
  46. insert into teacher values( '856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');
  47. insert into teacher values( '825', '王萍', '女', '1972-05-05', '助教', '计算机系');
  48. insert into teacher values( '831', '刘冰', '女', '1977-08-14', '助教', '电子工程系');
  49. #添加课程表
  50. insert into course values( '3-105', '计算机导论', '825');
  51. insert into course values( '3-245', '操作系统', '804');
  52. insert into course values( '6-166', '数字电路', '856');
  53. insert into course values( '9-888', '高等数学', '831');
  54. #添加成绩表
  55. insert into score values( '103', '3-245', '86');
  56. insert into score values( '105', '3-245', '75');
  57. insert into score values( '109', '3-245', '68');
  58. insert into score values( '103', '3-105', '92');
  59. insert into score values( '105', '3-105', '88');
  60. insert into score values( '109', '3-105', '76');
  61. insert into score values( '103', '3-105', '64');
  62. insert into score values( '105', '3-105', '91');
  63. insert into score values( '109', '3-105', '78');
  64. insert into score values( '103', '6-166', '85');
  65. insert into score values( '105', '6-166', '79');
  66. insert into score values( '109', '6-166', '81');



 
   

 


    
    
  1. 1、 查询Student表中的所有记录的Sname、Ssex和Class列。
  2. select Sname,Ssex, Class from student
  3. 2、 查询教师所有的单位即不重复的Depart列。
  4. select distinct Depart fromteacher
  5. 3、 查询Student表的所有记录。
  6. select * from student
  7. 4、 查询Score表中成绩在 6080之间的所有记录。
  8. select * from Score where Degree between60and 80
  9. 5、 查询Score表中成绩为 858688的记录。
  10. select * from Score where Degree in( 85, 86, 88)
  11. 6、 查询Student表中“ 95031”班或性别为“女”的同学记录。
  12. select * from Student where class= '95031'orSsex= '女'
  13. 7、 以 Class降序查询Student表的所有记录。
  14. select * from student order by class desc
  15. 8、 以Cno升序、Degree降序查询Score表的所有记录。
  16. select * from Score order by cno asc,degree desc
  17. 9、 查询“ 95031”班的学生人数。
  18. select count(*) fromstudentwhere class= '95031'
  19. 10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
  20. select Sno,Cno from ScorewhereDegree=(selectmax(Degree)fromScore)
  21. select Sno,Cno from Scoreorderby Degree desclimit 0, 1
  22. 11、查询每门课的平均成绩。
  23. select Cno, avg(degree) from Score groupbyCno
  24. 12、查询Score表中至少有 5名学生选修的并以 3开头的课程的平均分数。
  25. select avg(Degree) fromscorewhere Cno like '3%'andCno in(selectCno from score group byCno having count(*)>= 5) 用 in不用= 是因为可能会有多个
  26. 简单写法:selectavg(Degree)fromscore where Cno like '3%' and group by Cno havingcount(*)>= 5
  27. 13、查询分数大于 70,小于 90的Sno列。
  28. select Sno from Score where degree> 70 anddegree< 90
  29. 14、查询所有学生的Sname、Cno和Degree列。
  30. select Sname, Cno,Degree from Score , student where Score.Sno=student.Sno
  31. 15、查询所有学生的Sno、Cname和Degree列。
  32. select Sno,Cname,Degree from Score , Course where Score.Cno=Course.Cno
  33. 16、查询所有学生的Sname、Cname和Degree列。
  34. select Sname,Cname,Degree from student,course,score where student.Sno=score.Sno and course.Cno=score.Cno join .. on
  35. 写法:selectSname,Cname,Degreefromstudent join score onstudent.Sno=score.Snojoincourse on course.Cno=score.Cno
  36. 17、查询“ 95033”班学生的平均分。
  37. select avg(degree) as 'class=95033'fromScore whereSno in (selectSno from Student whereClass= '95033')  
  38. 18、 假设使用如下命令建立了一个grade表:
  39. create table grade( low int( 3),upp int( 3), rank char( 1))
  40. insert into grade values( 90, 100,’A’)
  41. insert into grade values( 80, 89,’B’)
  42. insert into grade values( 70, 79,’C’)
  43. insert into grade values( 60, 69,’D’)
  44. insert into grade values( 0, 59,’E’)
  45. 现查询所有同学的Sno、Cno和 rank列。
  46. select Sno,Cno, rank from Score,grade where degree between lowandupp
  47. 19、查询选修“ 3 -105”课程的成绩高于“ 109”号同学成绩的所有同学的记录。
  48. 109同学,选修是 3 -105课的
  49. select * from score where Cno= '3-105'anddegree>(selectmax(degree )fromScore where Sno= '109'andCno= '3-105')
  50. 109同学,没有选修 3 -105
  51. select * from score where Cno= '3-105'anddegree>(selectmax(degree )fromScore where Sno= '109')
  52. and degree<( selectmax(degree )fromScore where sno in(selectSnofrom score group bySnohaving count(*)> 1))
  53. 选了多门课程并且是这个课程下不是最高分的
  54. select * from score a where Sno in(selectSno from score group bySno having count(*)> 1)anddegree<( select max(degree )fromScore b where b.cno = a.cno)
  55. 21、查询成绩高于学号为“ 109”、课程号为“ 3 -105”的成绩的所有记录。
  56. Select * from score where degree>(selectdegreefrom Score where Sno= '109'andCno= '3-105')
  57. 22、查询和学号为 108101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
  58. select sno,sname,sbirthday from student where year(sbirthday) = (selectyear(sbirthday)fromstudent where sno= '108')
  59. select sno,sname,sbirthday from student where year(sbirthday) = (selectyear(sbirthday)fromstudent where sno= '101')
  60. 23、查询“张旭“教师任课的学生成绩。
  61. select Sno,degree from score,Course wherescore.Cno=Course.CnoandCourse.Tno= (selectTnofrom Teacher where Tname= '张旭')
  62. select degree from scorewhereCno in (selectcno from course whereTno= (selectTnofrom Teacher where Tname= '张旭' ) )
  63. 24、查询选修某课程的同学人数多于 5人的教师姓名。
  64. select Tname from Teacher, CoursewhereTeacher.Tno=Course.TnoandCourse.Cno =(selectCnofrom Score group byCnohaving count(*)> 5)
  65. select Tname from Teacherwheretno=( select Tno fromCourse where cno=( selectCno from Score groupbyCno havingcount(*)> 5 ))
  66. 25、查询 95033班和 95031班全体学生的记录。
  67. select * from student where class in( '95033', '95031')
  68. 26、查询存在有 85分以上成绩的课程Cno.
  69. select Cno from score where degree> 85
  70. 27、查询出“计算机系“教师所教课程的成绩表。
  71. select * from course where cno in(selectcno from course where tnoin(selecttno from teacher where Depart= '计算机系'))
  72. 28、查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。
  73. select Tname,Prof from TeacherwhereDepart = '计算机系'andProf notin(selectProf from Teacher whereDepart = '电子工程系')<br> union<br>selectTname,Prof from Teacherwhere Depart = '电子工程系'andProf notin(selectProf from Teacher whereDepart = '计算机系')
  74. 29、查询选修编号为“ 3 -105“课程且成绩至少高于选修编号为“ 3 -245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
  75. any:代表括号中任意一个成绩就可以
  76. select Cno,Sno,Degree from score where cno= '3-105'anddegree > any(selectdegreefrom score where cno= '3-245') order bydegree desc
  77. 30、查询选修编号为“ 3 -105”且成绩高于选修编号为“ 3 -245”课程的同学的Cno、Sno和Degree.
  78. all:代表括号中的所有成绩
  79. select Cno,Sno,Degree from score where cno= '3-105'anddegree >all(selectdegreefrom score where cno= '3-245') order bydegree desc
  80. 31、查询所有教师和同学的 name、sex和birthday.
  81. select tname,tsex,tbirthday from Teacher union selectsname,ssex,sbirthdayfromStudent
  82. 32、查询所有“女”教师和“女”同学的 name、sex和birthday.
  83. select Tname,Tsex,Tbirthday from Teacher where Tsex= '女'unionselect Sname,Ssex,Sbirthdayfrom Student where Ssex= '女'
  84. 33、查询成绩比该课程平均成绩低的同学的成绩表。
  85. select * from score a where degree < (selectavg(degree)fromscore b where b.cno=a.cno)
  86. 34、查询所有任课教师的Tname和Depart.
  87. select Tname,Depart from Teacher where tnoin(selecttnofrom course )
  88. 35、查询所有未讲课的教师的Tname和Depart.
  89. select Tname,Depart from Teacher where Tnonotin (selectTno from Course wherecno in (selectcno from score ))
  90. 36、查询至少有 2名男生的班号。
  91. select class from studentwheressex= '男'groupby class havingcount(*)> 1
  92. 37、查询Student表中不姓“王”的同学记录。
  93. select * from Student where Sname not like '王%%'
  94. 38、查询Student表中每个学生的姓名和年龄。
  95. select Sname, year( now())- year(sbirthday) fromStudent
  96. 39、查询Student表中最大和最小的Sbirthday日期值。
  97. select Max(Sbirthday ), Min(Sbirthday )fromStudent
  98. 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
  99. select * from Student order by class desc, Sbirthday
  100. 41、查询“男”教师及其所上的课程。
  101. select Tname,Cname from course,teacher where course.tno= teacher.tno and teacher.Tsex= '男'
  102. 42、查询最高分同学的Sno、Cno和Degree列。
  103. select Sno,Cno,Degree from score where degree=(selectmax(degree)fromscore)
  104. 排序写法: select Sno,Cno,Degree from score order bydegreedesc limit 0, 1
  105. 43、查询和“李军”同性别的所有同学的Sname.
  106. select Sname from StudentwhereSsex = (selectSsexfrom Student where Sname= '李军')
  107. 44、查询和“李军”同性别并同班的同学Sname.
  108. select Sname from StudentwhereSsex = (selectSsexfrom Student where Sname= '李军') and class=( select classfromstudent where Sname= '李军')
  109. 45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
  110. select Sno,Cno,degree from score where Cno=(selectCno from course whereCname= '计算机导论')andSno in(selectSno from student where Ssex= '男')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值