练习. SQL--学生教师分数
Posted 庚xiao午
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习. SQL--学生教师分数相关的知识,希望对你有一定的参考价值。
create database Homework_0609 use Homework_0609 --表一:学生表 student --学号:code int (主键)从1开始 --姓名:name varchar(50) --性别:gender char(10) --班级:class char(10) --语文教师编号:Chineseteacher int --数学教师编号:Mathteacher int --英语教师编号:Englishteacher int create table student ( scode int primary key identity (1,1), sname varchar(10), sgender char(10), sclass char(10), sChineseteacher int, sMathteacher int, Englishteacher int ) --表二:教师表 teacher --教师编号:code int (主键) 从1001开始 --姓名:name varchar(20) --负责课程:lesson char(10)(语文、数学、英语) --年龄:age int --生日:birth datetime create table teacher ( tcode int primary key identity (1001,1), tlesson char(10), tage int, tbirth datetime, tname varchar(20) ) --表三:分数表 score --学号:code int (学生学号的外键) --语文分数:Chinese decimal(18,2) --数学分数:Math decimal(18,2) --英语分数:English decimal(18,2) create table score ( score_code int, score_Chinese decimal (18,2), score_Math decimal (18,2), score_English decimal (18,2) ) --学生表数据:插入三个班的学生数据,每个班的相同科目的教师都是相同的 --至少每个班5个人 insert into student values(\'赵一\',\'男\',\'一班\',1001,1004,1007) insert into student values(\'钱二\',\'男\',\'一班\',1001,1004,1007) insert into student values(\'孙三\',\'女\',\'一班\',1001,1004,1007) insert into student values(\'李四\',\'男\',\'一班\',1001,1004,1007) insert into student values(\'周五\',\'男\',\'一班\',1001,1004,1007) insert into student values(\'吴六\',\'女\',\'二班\',1002,1005,1008) insert into student values(\'郑七\',\'女\',\'二班\',1002,1005,1008) insert into student values(\'王八\',\'男\',\'二班\',1002,1005,1008) insert into student values(\'冯九\',\'男\',\'二班\',1002,1005,1008) insert into student values(\'陈十\',\'男\',\'二班\',1002,1005,1008) insert into student values(\'褚十一\',\'女\',\'三班\',1003,1006,1009) insert into student values(\'卫十二\',\'女\',\'三班\',1003,1006,1009) insert into student values(\'蒋十三\',\'女\',\'三班\',1003,1006,1009) insert into student values(\'沈十四\',\'男\',\'三班\',1003,1006,1009) insert into student values(\'韩十五\',\'男\',\'三班\',1003,1006,1009) insert into student values(\'杨十六\',\'女\',\'三班\',1003,1006,1009) select * from student --教师表数据:语文教师,数学教师,英语教师分别三个,每一个对应一个班级 insert into teacher values(\'语文\',30,\'1986-08-08\',\'语一\') insert into teacher values(\'语文\',31,\'1985-08-08\',\'语二\') insert into teacher values(\'语文\',32,\'1984-08-08\',\'语三\') insert into teacher values(\'数学\',38,\'1978-08-08\',\'数一\') insert into teacher values(\'数学\',28,\'1988-08-08\',\'数二\') insert into teacher values(\'数学\',33,\'1983-08-08\',\'数三\') insert into teacher values(\'英语\',30,\'1986-08-08\',\'英一\') insert into teacher values(\'英语\',25,\'1991-08-08\',\'英二\') insert into teacher values(\'英语\',40,\'1976-08-08\',\'英三\') select * from teacher --分数表数据:学号对应的语文、数学、英语成绩 insert into score values (1,98.5,60,83.5) insert into score values (2,25,66,90.5) insert into score values (3,88.5,100,85) insert into score values (4,98,90,83) insert into score values (5,90,69,95) insert into score values (6,85,90,80) insert into score values (7,68.5,70,83) insert into score values (8,55.5,60,85) insert into score values (9,75,60,55) insert into score values (10,91,69,95) insert into score values (11,95,90,80.5) insert into score values (12,80,80,83.5) insert into score values (13,50,70,80) insert into score values (14,95,77.5,70) insert into score values (15,80,99,65) insert into score values (16,98.5,100,95.5) select * from score --查询此次语文成绩最高的学生的信息 select * from student where scode= (select top 1 score_code from score order by score_Chinese desc)
--查询此次数学成绩最高的学生的信息 select * from student where scode= (select top 1 score_code from score order by score_Math desc)
--查询此次英语成绩最高的学生的信息 select * from student where scode= (select top 1 score_code from score order by score_English desc)
--查询此次语文成绩最低的学生的信息 select * from student where scode= (select top 1 score_code from score order by score_Chinese asc)
--查询此次数学成绩最低的学生的信息 select * from student where scode= (select top 1 score_code from score order by score_Math asc)
--查询此次英语成绩最低的学生的信息 select * from student where scode= (select top 1 score_code from score order by score_English asc)
--查询此次语文成绩最低的学生所任课教师的信息 select * from teacher where tcode= (select sChineseteacher from student where scode= (select top 1 score_code from score order by score_Chinese asc))
--查询此次数学成绩最低的学生所任课教师的信息 select * from teacher where tcode= (select sMathteacher from student where scode= (select top 1 score_code from score order by score_Math asc))
--查询此次英语成绩最低的学生所任课教师的信息 select * from teacher where tcode= (select Englishteacher from student where scode= (select top 1 score_code from score order by score_English asc))
--查询此次语文成绩最高的学生所任课教师的信息 select * from teacher where tcode= (select sChineseteacher from student where scode= (select top 1 score_code from score order by score_Chinese desc))
--查询此次数学成绩最高的学生所任课教师的信息 select * from teacher where tcode= (select sMathteacher from student where scode= (select top 1 score_code from score order by score_Math desc))
--查询此次英语成绩最高的学生所任课教师的信息 select * from teacher where tcode= (select Englishteacher from student where scode= (select top 1 score_code from score order by score_English asc))
--查询各个学生的学号,姓名,语文分数,数学分数,英语分数,以及三门课里面每一门课的任课教师姓名 select scode,sname,sChineseteacher,sMathteacher,Englishteacher from student select score_Chinese,score_Math,score_English from score
--查询每个班级里的语文最高分 select top 1 score_Chinese as \'一班语文最高分\' from score where score_code>=1 and score_code<=5 order by score_Chinese desc select top 1 score_Chinese as \'二班语文最高分\' from score where score_code>=6 and score_code<=10 order by score_Chinese desc select top 1 score_Chinese as \'三班语文最高分\' from score where score_code>=11 and score_code<=16 order by score_Chinese desc
--查询每个班级里的数学最高分 select top 1 score_Math as \'一班数学最高分\' from score where score_code>=1 and score_code<=5 order by score_Math desc select top 1 score_Math as \'二班数学最高分\' from score where score_code>=6 and score_code<=10 order by score_Math desc select top 1 score_Math as \'三班数学最高分\' from score where score_code>=11 and score_code<=16 order by score_Math desc
--查询每个班级里的英语最高分 select top 1 score_English as \'一班英语最高分\' from score where score_code>=1 and score_code<=5 order by score_English desc select top 1 score_English as \'二班英语最高分\' from score where score_code>=6 and score_code<=10 order by score_English desc select top 1 score_English as \'三班英语最高分\' from score where score_code>=11 and score_code<=16 order by score_English desc
--查看每个班的语文平均分 select AVG(score_Chinese) as \'一班语文平均分\' from score where score_code>=1 and score_code<=5 select AVG(score_Chinese) as \'二班语文平均分\' from score where score_code>=6 and score_code<=10 select AVG(score_Chinese) as \'三班语文平均分\' from score where score_code>=11 and score_code<=16
--查询学生信息,将所有语文任课教师编号改为该科目的任课教师名字显示 select scode,sname,sgender,sclass, (select tname from teacher where teacher.tcode=student.sChineseteacher) as 语文教师 from student
--查询学生信息,将所有任课教师编号改为该科目的任课教师名字显示 select scode,sname,sgender,sclass, (select tname from teacher where teacher.tcode=student.sChi以上是关于练习. SQL--学生教师分数的主要内容,如果未能解决你的问题,请参考以下文章