资源描述:
《SQL-server-查询语句-练习试题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、SQLserver查询语句练习题用SQL语句创建四个表:createdatabasetongjigousetongjigocreatetablestudent(Snovarchar(20)notnullprimarykey厂■学号Snamevarchar(20)notnull,—学生姓名Ssexvarchar(20)notnull,—学生性别Sbirthdaydatetime,--学生出生年月Classvarchar(20)—学生所在班级)gocreatetableteacher一老0帀(Tnovarchar(20)notnullprimarykey,一
2、教工编号(主码)Tnamevarchar(20)notnull,—教工姓名Tsexvarchar(20)notnull,―教工性别Tbirthdaydatetime,—教工出生年月Profvarchar(20)z--职称Departvarchar(20)notnull—教工所在部门)gocreatetableCourse—课程(Cnovarchar(20)notnullprimarykey,一课程号Cnamevarchar(20)notnull,--课程名称Tnovarchar(20)notnullreferencesteacher(Tno),一教工编号
3、(外码))gocreatetableScore—分数(Snovarchar(20)notnullreferencesstudent(Sno),■■学号(外码)Cnovarchar(20)notnullreferencesCourse(Cno),一课程号(外码)primarykey(Sno,Cno),DegreeDecimal(4zl)z—成绩)表中数据如下:表(_)StudentSnoSnameSsexSbirthdayclass108曾华男1977-09-0195033105匡明男1975-10-0295031107王丽女1976-01-2395033
4、101李军男1976-02-2095033109王芳女1975-02-1095031103陆君男1974-06-0395031表(二)CourseCnoCnameTno3-105计算机导论8253-245操作系统8046-166数字电路8569-888高等数学831表(三)ScoreSnoCnoDegree1033-245861053-245751093-245681033-105921053-10588E3-105761013-105641073-105911083-105781016-166851076-166791086-16681表(四)Teac
5、herTnoTnameTsexTbirthdayProfDepart804李诚男1958-12-02副教授计算机系856张旭男1969-03-12讲师电子工程系825王萍女1972-05-05助教计算机系831刘冰女1977-08-14助教电子工程系1>查询Student表中的所有记录的Sname.Ssex和Class列。selectSname,Ssex,Classfromstudent2、査询教师所有的单位即不重复的Depart列。selectdistinetdepartfromteacher3、査询Student表的所有记录。select*froms
6、tudent4、查询Score表中成绩在60到80之间的所有记录。select*fromscorewheredegreebetween60and805、査询Score表中成绩为85,86或88的记录。select*fromscorewheredegreein(85,86,88)6>查询Student表中、、95031"班或性别为、、女"的同学记录。select*fromstudentwhereclass—95031'orSsex—女‘7、以Class降序查询Student表的所有记录。select*fromstudentorderbyclassdesc8
7、、以Cno升序、Degree降序查询Score表的所有记录。select*fromscoreorderbycno,degreedesc9、査询、、95031."班的学生人数。selectcount(*)fromstudentwhereclass=,95031,10、査询Score表中的最高分的学生学号和课程号。(子査询或者排序)selectsno’cnofromscorewheredegree=(selectmax(degree)fromscore)11、查询每门课的平均成绩。selectcno,avg(degree)fromscoregroupbycn
8、o12、査询Score表中至少有5名学生选修的并以3开头的课程的平均分数。sel