最完整的sql练习+答案上课讲义.docx

上传人:b****1 文档编号:421762 上传时间:2022-10-10 格式:DOCX 页数:17 大小:241.55KB
下载 相关 举报
最完整的sql练习+答案上课讲义.docx_第1页
第1页 / 共17页
最完整的sql练习+答案上课讲义.docx_第2页
第2页 / 共17页
最完整的sql练习+答案上课讲义.docx_第3页
第3页 / 共17页
最完整的sql练习+答案上课讲义.docx_第4页
第4页 / 共17页
最完整的sql练习+答案上课讲义.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

最完整的sql练习+答案上课讲义.docx

《最完整的sql练习+答案上课讲义.docx》由会员分享,可在线阅读,更多相关《最完整的sql练习+答案上课讲义.docx(17页珍藏版)》请在冰豆网上搜索。

最完整的sql练习+答案上课讲义.docx

最完整的sql练习+答案上课讲义

 

最完整的sql练习+答案

练习题一

createdatabasemydb

go

usemydb

createtablestudent

--学号

snovarchar(3)notnullprimarykey,

--姓名

snamevarchar(4)notnull,

--性别

ssexvarchar

(2)notnull,

--出生年月

sbirthdaydatetime,

--所在班级

classvarchar(5)

createtableteacher

--教工编号

tnovarchar(3)notnullprimarykey,

--教工姓名

tnamevarchar(4)notnull,

--教工性别

tsexvarchar

(2)notnull,

--教工出生日期

tbirthdaydatetime,

--职称

profvarchar(6),

--所在部门

departvarchar(10)

createtablecourse

--课程号

cnovarchar(5)notnullprimarykey,

--课程名称

cnamevarchar(10)notnull,

--教工编号

tnovarchar(3)referencesteacher(tno)

createtablescore

--学号

snovarchar(3)notnullreferencesstudent(sno),

--课程号

cnovarchar(5)notnullreferencescourse(cno),

--成绩

degreedecimal(4,1)

insertintostudent

values('108','曾华','男','1977-09-01','95033')

insertintostudent

values('105','匡明','男','1975-10-02','95031')

insertintostudent

values('107','王丽','女','1976-01-23','95033')

insertintostudent

values('101','李军','男','1976-02-20','95033')

insertintostudent

values('109','王芳','女','1975-02-10','95031')

insertintostudent

values('103','陆君','男','1974-06-03','95031')

insertintoteacher

values('804','李诚','男','1958-12-02','副教授','计算机系')

insertintoteacher

values('856','张旭','男','1969-03-12','讲师','电子工程系')

insertintoteacher

values('825','王萍','女','1972-05-05','助教','计算机系')

insertintoteacher

values('831','刘冰','女','1958-08-14','助教','电子工程系')

insertintocourse

values('3-105','计算机导论','825')

insertintocourse

values('3-245','操作系统','804')

insertintocourse

values('6-166','数字电路','856')

insertintocourse

values('9-888','高等数学','831')

insertintoscore

values('103','3-245','86')

insertintoscore

values('105','3-245','75')

insertintoscore

values('109','3-245','68')

insertintoscore

values('103','3-105','92')

insertintoscore

values('105','3-105','88')

insertintoscore

values('109','3-105','76')

insertintoscore

values('101','3-105','64')

insertintoscore

values('107','3-105','91')

insertintoscore

values('108','3-105','78')

insertintoscore

values('101','6-166','85')

insertintoscore

values('107','6-166','79')

insertintoscore

values('108','6-166','81')

select*fromstudent

select*fromteacher

select*fromcourse

select*fromscore

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

selectsname,ssex,classfromstudent

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

selectdistinctdepartfromteacher

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

select*fromstudent

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

select*fromscorewheredegreebetween60and80

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

select*fromscorewheredegree='85'ordegree='86'ordegree='88'

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

select*fromstudentwhereclass='95031'orssex='女'

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

select*fromstudentorderbyclassdesc

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

select*fromscoreorderbycno,degreedesc

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

selectcount(sno)fromstudentwhereclass='95031'

--10、查询Score表中的最高分的学生学号和课程号。

selectsno,cno,degreefromscorewheredegreein(selectmax(degree)fromscore)

--11、查询‘3-105’号课程的平均分。

selectavg(degree)fromscorewherecno='3-105'

--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

selectavg(degree)fromscorewherecnolike'3%'andcnoin(selectcnofromscoregroupbycnohavingcount(cno)>5)

--13、查询最低分大于70,最高分小于90的Sno列。

selectsnofromscorewheredegreebetween70and90

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

selectsname,cno,degreefromscore,studentwherestudent.sno=score.sno

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

selectcname,student.sno,degreefromscore,student,coursewherestudent.sno=score.snoando=o

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

selectsname,cname,degreefromscore,student,coursewherestudent.sno=score.snoando=o

--17、查询“95033”班所选课程的平均分。

select平均分=avg(degree)fromcourse,student,scorewhereclass='95033'ando=oandstudent.sno=score.sno

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

--createtablegrade(lowint,uppint,rankvarchar

(1))

--insertintogradevalues(90,100,'A')

--insertintogradevalues(80,89,'B')

--insertintogradevalues(70,79,'C')

--insertintogradevalues(60,69,'D')

--insertintogradevalues(0,59,'E')

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

selectstudent.sno,cno,rankfromscore,student,gradewherestudent.sno=score.snoanddegreebetweenlowandupp

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

//无关子查询

selectscore.sno,sname,ssex,sbirthday,class,o,cname,degreefromscore,student,coursewherestudent.sno=score.snoando=oando='3-105'anddegree>

(selectdegreefromscorewheresno='109'andcno='3-105')

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

selectsno,cno,degreefromscorewheredegreenotin(selectmax(degree)fromscoregroupbycno)orderbysno

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

select*fromscorewheredegree>(selectdegr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1