sql习题及答案
SQL语句练习及答案

sql语句练习题1数据库有如下四个表格:student(sno,sname,sage,ssex,sdpt) 学生表系表(dptno,dname)course(cno,cname, gradet, tno) 课程表sc(sno,cno,score) 成绩表teacher(tno,tname) 教师表要求:完成以下操作1.查询姓"欧阳"且全名为三个汉字的学生的姓名。
select sname from student where sname like “欧阳__‟;2.查询名字中第2个字为"阳"字的学生的姓名和学号。
select sname,sno from student where sname like '_阳%';3.查询所有不姓刘的学生姓名。
select sname,sno,ssexfrom studentwhere sname not like “刘%”;4.查询db_design课程的课程号和学分。
select cno,ccredit from coursewhere cname like 'db_design'5.查询以"db_"开头,且倒数第3个字符为i的课程的详细情况。
select * from course where cname like 'db%i_ _';6.某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。
查询缺少成绩的学生的学号和相应的课程号。
select sno,cno from sc where grade is null;7.查所有有成绩的学生学号和课程号。
select sno,cno from sc where grade is not null;8.查询计算机系年龄在20岁以下的学生姓名。
select sname from student where sdept= 'cs' and sage<20;9.查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。
3-SQL语句练习题参考答案

标准SQL语言一、选择题1、下面关于SQL标准的叙述中,不正确的是(B )。
A.SQL语言是集数据定义、数据操纵、数据控制功能为一体的语言。
B.SQL语言是一种高度过程化的语言。
C.SQL标准规定数据库是按三级模式结构构建。
D.SQL语言是关系型数据库的标准语言。
E.SQL语言是面向集合的语言。
2、SQL语言中,修改基本表结构的语句是( B )。
A.UPDATE B.ALTER C.DROP D.CREATE3、SQL语言中,删除基本表结构的语句是( C )。
A.DELETE B.ALTER C.DROP D.CREATE4、下面关于“视图”的叙述中,不正确的是( C )。
A.视图是一种“虚表”,它的数据被存放在基本表中。
B.视图提供了逻辑数据独立性。
C.不能通过视图来更新数据库中的数据。
D.视图能提供对数据的安全保护。
5、下面关于SELECT语句的叙述中,不正确的是(C)。
A.SELECT产生的结果是一个集合。
B.HA VING子句必须与GROUP BY子句一起使用。
C.可以省略FROM子句。
D.可以省略WHERE子句。
二、填空题1、在使用INSERT语句向一个表中插入元组时,“值列表”中值的个数、(顺序)、类型必须与“列名表”保持一致。
2、在向一个表中插入元组时,对于未指定默认值且(不能取空值)的字段必须赋值。
3、向表中插入元组时,主键的值不能取(NULL )值。
4、在使用DELETE语句时,如果不指定(where 条件)就会将整个表的数据删除。
5、视图是从一个或几个基本表或(视图)导出的表,它与基本表不同,是一个虚表。
三、判断题1、SQL语言是面向集合操作的语言。
√2、可以通过视图来查询数据,但不能通过视图来更新数据库中的数据。
×3、在SQL Server数据库系统中,向表中插入元组时,系统自动为具有标识属性的列赋值。
√4、在SQL Server数据库系统中,向表中插入元组时,对取值类型为timestamp(时间戳)的列不能赋值,系统自动赋值。
SQL查询习题及答案

36.设教学用的四个基本表(S,C,T,SC)(2)查询年龄大于23岁的女同学的学号和姓名select 姓名,学号from swhere 性别='女' and 年龄>23(3)查询至少选修了刘老师所讲授课程中的一门课程的女同学姓名select s.姓名from s,scwhere s.性别='女'and s.学号=sc.学号and sc.课程编号in(select c.课程编号from c,twhere c.教师编号=t.教师编号and t.姓名='刘%')(4)查询至少选修了2门课程的学生学号select sc.学号from scgroup by 学号having count(课程编号)>2(5)查询全部学生都选修的课程号与课程名select c.课程编号,c.课程名称from c,scwhere sc.课程编号=c.课程编号and sc.学号=(select distinct s.学号from s)(6)计算机系每个教师讲授的课程号select t.教师编号,课程编号from c,twhere t.所在系='计算机系(7)查询没有选修过任何一门课程的学生的学号select s.学号from swhere s.学号not in(select distinct sc.学号from sc)(10)统计个系教师的人数select count (教师编号)from tgroup by 所在系(11)统计出教师人数超过10人的系的名称select t.所在系from tgroup by 所在系having count(教师编号)>10(12)在选课表SC中查询成绩为NULL的学生的学号和课程号select 学号课程编号from scwhere 成绩='NULL'(13)姓王的同学的年龄、姓名、选课名称、成绩select 年龄,姓名,课程名称,成绩from s,c,scwhere s.学号=sc.学号and c.课程编号=sc.课程编号and s.姓名='王%'(14)查询年龄大于女同学平均年龄的男同学姓名和年龄select 姓名,年龄from swhere 性别='男' and 年龄>(select avg(年龄)from swhere 性别='女')37.在数据库{USER、ORDER}中,用户需要查询“所有于2009年5月25日下订单的女顾客姓名”。
《数据库系统概论》SQL语言复习题(含答案)

SQL语言习题一、单项选择题1.SQL语言是的语言,易学习。
A.过程化B.非过程化C.格式化D.导航式答案:B2.SQL语言是语言。
A.层次数据库B.网络数据库C.关系数据库D.非数据库答案:C3.SQL语言具有的功能。
A.关系规范化、数据操纵、数据控制B.数据定义、数据操纵、数据控制C.数据定义、关系规范化、数据控制D.数据定义、关系规范化、数据操纵答案:B4.SQL语言的数据操纵语句包括SELECT,INSERT,UPDATE和DELETE等。
其中最重要的,也是使用最频繁的语句是。
A.SELECT B.INSERT C.UPDA TE D.DELETE答案:A5.SQL语言具有两种使用方式,分别称为交互式SQL和。
A.提示式SQL B.多用户SQL C.嵌入式SQL D.解释式SQL答案:C6.SQL语言中,实现数据检索的语句是。
A.SELECT B.INSERTC.UPDATE D.DELETE答案:A7.下列SQL语句中,修改表结构的是。
A.ALTER B.CREATEC.UPDATE D.INSERT答案:A第8到第11题基于这样的三个表即学生表S、课程表C和学生选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)其中:S#为学号,SN为姓名,SEX为性别,AGE为年龄,DEPT为系别,C#为课程号,CN为课程名,GRADE为成绩。
8.检索所有比“王华”年龄大的学生姓名、年龄和性别。
正确的SELECT语句是。
A.SELECT SN,AGE,SEX FROM SWHERE AGE>(SELECT AGE FROM SWHERE SN=’王华’)B.SELECT SN,AGE,SEX FROM SWHERE SN=’王华’C.SELECT SN,AGE,SEX FROM SWHERE AGE>(SELECT AGEWHERE SN=’王华’)D.SELECT SN,AGE,SEX FROM SWHERE AGE>王华.AGE答案:A9.检索选修课程“C2”的学生中成绩最高的学生的学号。
(完整版)SQL复习题(附部分答案)

一、单选题1.根据关系数据基于的数据模型——关系模型的特征判断下列正确的一项:(___)A.只存在一对多的实体关系,以图形方式来表示。
B.以二维表格结构来保存数据,在关系表中不允许有重复行存在。
C.能体现一对多、多对多的关系,但不能体现一对一的关系。
D.关系模型数据库是数据库发展的最初阶段。
答案[B]2、在“连接”组中有两种连接认证方式,其中在(___)方式下,需要客户端应用程序连接时提供登录时需要的用户标识和密码。
A、Windows身份验证B、SQL Server 身份验证C、以超级用户身份登录时D、其他方式登录时答案[B]3、关系数据库中,主键是(___),当运用Transact-SQL语句创建主键时,可以是(__)。
⑴ A、为标识表中唯一的实体 B、创建唯一的索引,允许空值C、只允许以表中第一字段建立D、允许有多个主键的⑵ A、create table table1(column1 char(13) not null primary key,column2 int not on primary key;B、alter table table1 with nocheck addconstraint [PK_table1] primary key nonclustered ( column1) on primary;C、alter table table1 column1 primary key ;答案[ A C]4、视图是一种常用的数据对象,可以简化数据库操作,当使用多个数据表来建立视图时,不允许在该语句中包括(___)等关键字。
A、ORDER BY,COMPUTEB、ORDER BY,COMPUTE,COMPUTR BYC、ORDER BY,COMPUTE BY,GROUP BYD、GROUP BY,COMPUTE BY答案[B ]5、在Transact-SQL语法中,SELECT语句的完整语法较复杂,但至少包括的部分(_1_),使用关键字(_2_)可以把重复行屏蔽,将多个查询结果返回一个结果集合的运算符是(_3__),如果在SELECT语句中使用聚合函数时,可以在后面使用(_4_)。
(完整版)sql练习题+答案

(一) 新建以下几个表student(学生表):其中约束如下:(1)学号不能存在相同的(2)名字为非空(3)性别的值只能是’男’或’女’(4)系包括这几个:信息系,计算机科学系,数学系,管理系,中文系,外语系,法学系(5)出生日期为日期格式(6)年龄为数值型,且在0~100之间cs(成绩表):其中约束如下:(1)sno和cno分别参照student和course表中的sno,cno的字段(2)cj(成绩)只能在0〜100之间,可以不输入值create table cs (sno smallint not null referencesstudent ( sno ), ----定义成外键cno smallint not null referencescourse ( cno ), ----定义成外键cj smallint constraint e check (cj between0 and 100 ),---- 检查约束一—j(成绩)只能在~100之间,可以不输入值constraint f primary key ( sno , cno )---- 定义学生学号和课程号为sc表的主键)course(课程表)其约束如下:(1)课程号(cno)不能有重复的(2)课程名(cname非空(三)针对学生课程数据库查询(1)查询全体学生的学号与姓名。
Select sno , sname from student(2)查询全体学生的姓名、学号、所在系,并用别名显示出结果。
(3)查询全体学生的详细记录。
select * from student(4)查全体学生的姓名及其出生年份。
select sname , birth from student(5)查询学校中有哪些系。
select distinct dept from student(6)查询选修了课程的学生学号。
select sno from cs where eno is not null(7)查询所有年龄在20岁以下的学生姓名及其年龄。
第三章 SQL语言练习题和答案

第三章SQL语言一、选择题1. SQL语言是(B )的语言,容易学习。
A.过程化B. 非过程化C.格式化D. 导航式2. SQL语言的数据操纵语句包括SELECT、INSERT、UPDATE、DELETE等。
其中最重要的,也是使用最频繁的语句是(A)。
A. SELECTB. INSERTC. UPDATED. DELETE3. 在视图上不能完成的操作是()。
A. 更新视图B. 查询C. 在视图上定义新的表D. 在视图上定义新的视图4. SQL语言集数据查询、数据操纵、数据定义和数据控制功能于一体,其中,CREATE、DROP、ALTER语句是实现哪种功能()。
A. 数据查询B. 数据操纵C. 数据定义D. 数据控制5. SQL语言中,删除一个视图的命令是()。
A.DELETEB.DROPC.CLEARD.REMOVE6. 在SQL语言中的视图VIEW是数据库的()。
A. 外模式B. 模式C. 内模式D. 存储模式7. 下列的SQL语句中,()不是数据定义语句。
A. CREATE TABLEB. DROP VIEWC. CREATE VIEWD. GRANT8. 若要撤销数据库中已经存在的表S,可用()。
A. DELETE TABLE SB. DELETE SC. DROP TABLE SD. DROP S9. 若要在基本表S中增加一列CN(课程名),可用()。
A.ADD TABLE S(CN CHAR(8))B.ADD TABLE S ALTER(CN CHAR(8))C.ALTER TABLE S ADD(CN CHAR(8))D.ALTER TABLE S (ADD CN CHAR(8))10. 学生关系模式S(S#,Sname,Sex,Age),S的属性分别表示学生的学号、姓名、性别、年龄。
要在表S中删除一个属性“年龄”,可选用的SQL语句是()。
A. DELETE Age from SB. ALTER TABLE S DROP AgeC. UPDATE S AgeD. ALTER TABLE S …Age‟11. 有关系S(S#,SNAME,SAGE),C(C#,CNAME),SC(S#,C#,GRADE)。
SQL语言习题参考答案

第3章 SQL语言习题参考答案1.试述SQL语言的特点。
(85页)答:综合统一、高度非过程化、面向集合的操作方式、以同一种语法结构提供两种使用方式、语言简捷,易学易用。
2.试述SQL的定义功能。
(87页)答:SQL的数据定义功能包括定义表、定义视图和定义索引3.用SQL语句建立第二章习题5中的四个表。
S(SNO,SNAME,STATUS,CITY);P(PNO,PNAME,COLOR,WEIGHT);J(JNO,JNAME,CITY);SPJ(SNO,PNO,JNO,QTY);供应商表S由供应商代码(SNO)、供应商姓名(SNAME)、供应商状态(STATUS)、供应商所在城市(CITY)组成:CREATE TABLE S(Sno C(2) UNIQUE,Sname C(6) ,Status N(2),City C(4))零件表P由零件代码(PNO)、零件名(PNAME)、颜色(COLOR)、重量(WEIGHT)组成:CREATE TABLE P(Pno C(2) UNIQUE,Pname C(6),COLOR C(2), WEIGHT I(2))工程项目表J由工程项目代码(JNO)、工程项目名(JNAME)、所在城市(CITY)组成:CREATE TABLE J(Jno C(2) UNlQUE,JNAME C(8), CITY C(4))供应情况表SPJ由供应商代码(SNO)、零件代码(PNO)、工程项目代码(JNO)、供应数量(QTY)组成:CREATE TABLE SPJ(Sno C(2),Pno C(2),JNO C(2), QTY N(2))4.针对上题中建立的四个表试用SQL语言完成第二章习题5中的查询:(1)求供应工程J1零件的供应商号码SNO:SELECT DIST SNO FROM SPJ WHERE JNO=’J1’(2)求供应工程J1零件P1的供应商号码SNO:SELECT DIST SNO FROM SPJ WHERE JNO='J1' AND PNO='P1'(3)求供应工程J1零件为红色的供应商号码SNO:SELECT SNO FROM SPJ,P WHERE JNO='J1' AND = AND COLOR='红'(4)求没有使用天津供应商生产的红色零件的工程号JNO:SELECT DIST JNO FROM SPJ WHERE JNO NOT IN (SELE JNO FROM SPJ,P,S WHERE ='天津' AND COLOR='红' AND = AND =。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
题1、查询Student表中得所有记录得Sname、Ssex与Class列。
2、查询教师所有得单位即不重复得Depart列。
3、查询Student表得所有记录。
4、查询Score表中成绩在60到80之间得所有记录。
5、查询Score表中成绩为85,86或88得记录。
6、查询Student表中“95031”班或性别为“女”得同学记录。
7、以Class降序查询Student表得所有记录。
8、以Cno升序、Degree降序查询Score表得所有记录。
9、查询“95031”班得学生人数。
10、查询Score表中得最高分得学生学号与课程号。
11、查询‘3-105’号课程得平均分。
12、查询Score表中至少有5名学生选修得并以3开头得课程得平均分数。
13、查询最低分大于70,最高分小于90得Sno列。
14、查询所有学生得Sname、Cno与Degree列。
15、查询所有学生得Sno、Cname与Degree列。
16、查询所有学生得Sname、Cname与Degree列。
17、查询“95033”班所选课程得平均分。
18、假设使用如下命令建立了一个grade表:create table grade(low numeric(3,0),upp numeric(3),rank char(1));insert into grade values(90,100,'A');insert into grade values(80,89,'B');insert into grade values(70,79,'C');insert into grade values(60,69,'D');insert into grade values(0,59,'E');现查询所有同学得Sno、Cno与rank列。
19、查询选修“3-105”课程得成绩高于“109”号同学成绩得所有同学得记录。
20、查询score中选学一门以上课程得同学中分数为非最高分成绩得记录。
21、查询成绩高于学号为“109”、课程号为“3-105”得成绩得所有记录。
22、查询与学号为108得同学同年出生得所有学生得Sno、Sname与Sbirthday列。
23、查询“张旭“教师任课得学生成绩。
24、查询选修某课程得同学人数多于5人得教师姓名。
25、查询95033班与95031班全体学生得记录。
26、查询存在有85分以上成绩得课程Cno、27、查询出“计算机系“教师所教课程得成绩表。
28、查询“计算机系”与“电子工程系“不同职称得教师得Tname与Prof。
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”得同学得Cno、Sno与Degree,并按Degree从高到低次序排序。
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程得同学得Cno、Sno与Degree、31、查询所有教师与同学得name、sex与birthday、32、查询所有“女”教师与“女”同学得name、sex与birthday、33、查询成绩比该课程平均成绩低得同学得成绩表。
34、查询所有任课教师得Tname与Depart、35 查询所有未讲课得教师得Tname与Depart、36、查询至少有2名男生得班号。
37、查询Student表中不姓“王”得同学记录。
38、查询Student表中每个学生得姓名与年龄。
39、查询Student表中最大与最小得Sbirthday日期值。
40、以班号与年龄从大到小得顺序查询Student表中得全部记录。
41、查询“男”教师及其所上得课程。
42、查询最高分同学得Sno、Cno与Degree列。
43、查询与“李军”同性别得所有同学得Sname、44、查询与“李军”同性别并同班得同学Sname、45、查询所有选修“计算机导论”课程得“男”同学得成绩表下面就是参考答案:SQL语句练习题参考答案1、select sname,ssex,class from student;2、select distinct(depart) from teacher;orselect distinct depart from teacher;3、select * from student;4、select * from score where degree between 60 and 80;orselect * from score where degree>=60 and degree<=80;5、select * from score where degree in (85,86,88);orselect * from score where degree=85 or degree=86 or degree=88;6、select * from student where class=95031 or ssex='女';7、select * from student order by class desc;8、select * from score order by cno asc,degree desc;orselect * from score order by cno,degree desc;9、select count(*) from student where class=95031;orselect count(sno) from student where class=95031;10、select Sno as '学号',cno as '课程号', degree as '最高分' from score where degree=(select max(degree) from score);11、 select avg(degree) from score where cno='3-105';12、select cno,avg(degree) from score where cno like '3%' group by cno having count(sno)>5;orselect cno,avg(degree) from score where cno like '3%' group by cno having count(*)>5;13、select sno from score group by sno having min(degree)>70 and max(degree)<90;14、select student、sname,scoreo,score、degree from student,score where student、sno=score、sno; orselect sname,cno,degree from student,score where student、sno=score、sno;orselect x、sname,yo,y、degree from student x,score y where x、sno=y、sno;15、Select score、sno,courseame,score、degree from score,course where scoreo=courseo;orselect sno,cname,degree from score,course where scoreo=courseo;orselect x、sno,yame,x、degree from score x,course y where xo=yo;16、select student、sname,courseame,score、degree from student,course,score where student、sno=score、sno and courseo=scoreo;orselect sname,cname,degree from student,course,score where student、sno=score、sno and courseo=scoreo;orselect x、sname,yame,z、degree from student x,course y,score z where x、sno=z、sno and yo=zo;17、select cno,avg(degree) from score,student where student、sno=score、sno and class=95033 group by cno;orselect yo,avg(y、degree) from student x,score y where x、sno=y、sno and x、class=95033 group by yo;18、select sno,cno,rank from score,grade where degree between low and upp [order by rank]; [ ]表示可有可无19、select * from score where cno='3-105' and degree>(select degree from score where sno='109' and cno='3-105');orselect x、* from score x,score y where xo='3-105' and x、degree>y、degree and y、sno='109' and yo='3-105';20、分析:1、成绩非本科最高select * from score where degree not in (select max(degree) from score group by cno)选学一门以上得学生成绩:select sno from score group by sno having count(*)>1;2、查询成绩非本科最高并且选1门以上得学生得成绩:select * from score where degree not in(select max(degree) from score group by cno) group by sno having count(*)>1;orselect * from (select * from score where degree not in(select max(degree) from score group by cno)) as aa group by sno having count(*)>=2;通用答案:select sno from( select * from scorewhere degree not in(select max(degree) from score group by cno)) as aa group by sno having count(*)>=2;21、select * from score where degree>(select degree from score where sno=109 and cno='3-105'); orselect x、* from score x,score y where x、degree>y、degree and y、sno=109 and yo='3-105';22、select sno,sname,sbirthday from student where year(sbirthday)=(select year(sbirthday) from student where sno=108);23、select * from score where cno in(select cno from course where tno=(select tno from teacher where tname='张旭'));orselect cno,sno,degree from score where cno=(select xo from course x,teacher y where x、tno=y、tno and y、tname='张旭');24、select tname from teacher where tno in (select x、tno from course x,score y where xo=yo and yo in (select cno from score group by cno having count(*)>5));orselect tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having count(*)>5));orselect tname from teacher where tno in(select x、tno from course x,score y where xo=yo group by x、tno having count(x、tno)>5);25、select * from student where class in ('95033','95031');orselect * from student where class=95033 or class=95031;26、select distinct cno from score where degree in (select degree from score where degree>85);27、select * from score where cno in (select cno from course where tno in (select tno from teacher where depart='计算机系'));orselect * from score where cno in(select xo from course x,teacher y where y、tno=x、tno and y、depart='计算机系');28、select tname,prof from teacher where depart='计算机系' and prof not in (select prof from teacher where depart='电子工程系');29、select * from score where cno='3-105' and degree>(select min(degree) from score wherecno='3-245') order by degree desc;orselect * from score where cno='3-105'and degree>any(select degree from score where cno='3-245') order by degree desc;30、select * from score where cno='3-105' and degree>(select max(degree) from score wherecno='3-245');orselect * from score where cno='3-105' and degree>all(select degree from score wherecno='3-245');31、select sname as name,ssex as sex,sbirthday as birthday from studentunionselect tname,tsex,tbirthday from teacher;32、select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女' unionselect tname,tsex,tbirthday from teacher where tsex='女';33、select * from score a where degree<(select avg(degree) from score b where ao=bo);34、 select tname,depart from teacher where tno in(select tno from course);35、 select tname,depart from teacher where tno not in (select tno from course);36、select class from student where ssex='男' group by class having count(sno)>=2;37、 select * from student where sname not like '王%';38、select sname as '姓名',2010-year(sbirthday) as '年龄' from student;39、select sbirthday from student where sbirthday in (select min(sbirthday) from student) unionselect sbirthday from student where sbirthday in (select max(sbirthday) from student); orselect sbirthday from student where sbirthday=(select min(sbirthday) from student) or sbirthday=(select max(sbirthday) from student);40、select * from student order by class desc,sbirthday;41、select cname,tname from course,teacher where course、tno=teacher、tno and tsex='男';orselect courseame,teacher、tname from course,teacher where course、tno=teacher、tno and tsex='男';orselect cname,tname from course x,teacher y where x、tno=y、tno and y、tsex='男';orselect x、tname,yame from teacher x,course y where x、tno=y、tno and x、tsex='男';42、 select * from score where degree in (select max(degree) from score);43、select sname from student where ssex=(select ssex from student where sname='李军');44、 select sname from student where ssex=(select ssex from student where sname='李军') and class=(select class from student where sname='李军');45、select * from score where sno in(select sno from student where ssex='男') and cno in(select cno from course where cname='计算机导论');注意:20题得前两个答案在sql server 中不支持,在mysql中支持如果题答案中有错误,请记得及时通知我让我纠正错误!我得邮箱地址:。