教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 初中教育 >

SQL练习题参考答案

来源:网络收集 时间:2026-09-05
导读: 针对以上四个表,用SQL语言完成以下各项操作: ① 给学生表增加一属性Nation(民族),数据类型为Varchar(20); Alter table student add Nation Varchar(20); ② 删除学生表中新增的属性Nation; Alter table student drop column Nation; ③ 向成绩表中

针对以上四个表,用SQL语言完成以下各项操作:

① 给学生表增加一属性Nation(民族),数据类型为Varchar(20);

Alter table student

add Nation Varchar(20);

② 删除学生表中新增的属性Nation;

Alter table student

drop column Nation;

③ 向成绩表中插入记录(”2001110”,”3”,80);

insert into Grade

values('2001103','3',80);

④ 修改学号为”2001103”的学生的成绩为70分;

update Grade

set Gmark=70

where Sno='2001103';

⑤ 删除学号为”2001110”的学生的成绩记录;

delete Grade

where Sno='2001110';

⑥ 为学生表创建一个名为,以班级号的升序排序;

create index IX_Class on student(Clno ASC);

⑦删除IX_Class索引

Drop index IX_Class

针对以上四个表,用SQL语言完成以下各项查询:

① 找出所有被学生选修了的课程号;

select distinct cno

from grade;

② 找出01311班女学生的个人信息;

select *

from student

where clno=01311 and ssex='女';

③ 找出01311班、01312班的学生姓名、性别、出生年份;

Select sname,ssex, 2011-sage as ‘出生年份’

from student

where clno in('01311','01312');

④ 找出所有姓李的学生的个人信息;

Select *

from student

where sname like ’李%’;

⑤ 找出学生李勇所在班级的学生人数;

Select number

from class

where clno in (select clno

from student

where sname= '李勇');

⑥ 找出课程名为操作系统的平均成绩、最高分、最低分;

Select avg(gmark),max(gmark),min(gmark)

from grade

where cno in (select cno

from course

where cname='操作系统');

⑦ 找出选修了课程的学生人数;

Select count(distinct sno)

from grade;

⑧ 找出选修了课程操作系统的学生人数;

Select count(distinct sno)

from grade

where cno in (select cno

from course

where cname='操作系统');

⑨ 找出2000级计算机软件班的成绩为空的学生姓名。

Select sname

from student

where clno in (select clno

from class

where inyear=’2000’ and speciality=’计算机软件’)

and sno in (select sno

from grade

where gmark is null);

针对以上四个表,用SELECT的嵌套查询完成以下各项查询:

① 找出与李勇在同一个班级的学生信息;

Select *

from student

where clno in (select clno

from student

where sname=’李勇’);

② 找出所有与学生李勇有相同选修课程的学生信息;

Select *

from student

where sno in (select sno

from grade

where cno in ( select cno

from grade

where sno=(select sno

from student

where sname='李勇')))

③ 找出年龄介于学生李勇和25岁之间的学生信息;

Select sage

from student

where sage between (select sage

from student

where sname=’李勇’) and 25

④ 找出选修了课程操作系统的学生学号和姓名;

Select sno,sname

from student

where sno in (select sno

from grade

where cno in (select cno

from course

where cname=’操作系统’ ))

⑤ 找出所有没有选修1号课程的学生姓名;

Select sname

from student

where not exists (select *

from grade

where sno=student.sno and cno='1')

OR:

Select sname from student

Where sno not in

(Select sno from grade

Where cno=’1’);

⑥ 找出选修了全部课程的学生姓名。

(提示:可找出这样的学生,没有一门课程是他不选修的。)

Select sname

from student

where not exists (select * from course

where not exists (select * from grade

where sno=student.sno

and cno=http://www.77cn.com.cno))

解二:

Select sname from student

Where sno in

(Select sno from grade

Group by sno

Having count(*)=Select count(*) from course);

针对以上四个表,用SQL语言完成以下各项查询:

① 查询选修了3号课程的学生学号及其成绩,并按成绩的降序排列;

Select sno,gmark

from grade

where cno=’3’

order by gmark desc

② 查询全体学生信息,要求查询结果按班级号升序排列,同一班级学生按年龄降序排

列;

select *

from student

order by clno,sage desc

③ 求每个课程号及相应的选课人数;

Select cno ,count(sno)

from grade

group by cno

④ 查询选修了3门以上课程的学生学号。

Select sno

from grade

group by sno

having count(*)>3

针对以上四个表,用SQL语言完成以下各项操作:

① 将01311班的全体学生的成绩置零;

update grade

set gmark=0

where sno in (select sno

from student

where clno=’01311’)

② 删除2001级计算机软件的全体学生的选课记录;

delete

from grade

where sno in (select sno

from student

where clno=(select clno

from class

where speciality=’计算机软件’

and inyear=’2001’))

③ 学生李勇已退学,从数据库中删除有关他的记录。

delete

from grade

where sno in (select sno

from student

where sname='李勇')

update class

set monitor=null

where clno in (select clno

from student

where sname='李勇')

delete from student

where sname='李勇'

④对每个班,求学生的平均年龄,并把结果存入数据库;

create view v_sag

as select clno,AVG(sage) as ‘sag’

from student

group by clno)

视图操作:

① 建立01312班选修了1号课程的学生视图Stu_01312_1;

create view Stu_00312_1

as select *

from Student

where Student.Sno=any(select Sno from Grade

where http://www.77cn.com.cno='1')

and Student.Clno='00312'

②建立01312班选修了1号课程并且成绩不及格的学生视图Stu_01312_2;

create view Stu_00312_2

as select *

from Student

where Sno in (select Sno from Grade

where http://www.77cn.com.cno='1'and Grade.Gmark<='60')

and Clno='00312'

③建立视图Stu_year,由学生学号、姓名、 …… 此处隐藏:2603字,全部文档内容请下载后查看。喜欢就下载吧 ……

SQL练习题参考答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1564680.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)