SQL练习题参考答案
针对以上四个表,用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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [初中教育]婚姻家庭法学教学教案
- [初中教育]浅谈小学语文教学中的创新教育
- [初中教育]中华人民共和国侵权责任法2009
- [初中教育]2016-2022年中国薄膜太阳能电池行业发
- [初中教育]多级轻型井点降水的应用
- [初中教育]外语教学法流派介绍和简评
- [初中教育]实验一、典型环节及其阶跃响应
- [初中教育]内蒙古2012-2013学年度国家奖学金获奖
- [初中教育]移动通信营销渠道管理探讨
- [初中教育]初三化学第一学期第一第二章基础知识点
- [初中教育]一天的食物教学设计
- [初中教育]光导照明系统的基本结构及工作原理
- [初中教育]长春市十一高、东北师范大学附属中学、
- [初中教育]“十三五”规划重点-配重式装卸车项目
- [初中教育]领导方法和领导艺术
- [初中教育]第三章 植物病虫草鼠害诊断与防治基
- [初中教育]2019届九年级语文上册 第二单元 6纪念
- [初中教育]甲级单位编制水豆腐项目可行性报告(立
- [初中教育]Ch8-1补充 09101数据库系统原理及应用-
- [初中教育]2017-2023年中国吊装设备行业市场分析
- 制作毕业纪念册需要哪些材料
- 2015-2016学年高二化学苏教版选修4课件
- 哈佛管理导师-创建商业案例
- 职场交际中的谈吐礼仪知识与职场会议接
- 中国糕点及面包行业发展现状与竞争战略
- 沂河“12·7”洪水茶山拦河坝
- 管道水流量计算公式
- 4-2发电机火灾事故处置方案
- 数字信号处理实验五
- 2009年经济师(中级)金融专业知识全真试
- 历史街区保护规划--04历史文化遗产保护
- 宁夏回族自治区中小学职称评价标准
- 评先评优测评表
- 圆的切线证明及线段长求解在在中考中的
- 【解析版】2015年江苏省南京外国语学校
- 人教版八年级上册科学第一章习题精华
- 责任心与执行力
- SA8000社会责任管理体系标准培训
- IgA肾病的饮食应注意
- 杭州市建设工程文件归档整理方案(试行)




