SQL语句练习题答案

更新时间:2023-10-16 13:48:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

一 在数据库 school 中建立student , sc, course 表。

学生表、课程表、选课表属于数据库 school ,其各自的数据结构如下: 学生 student (sno,sname,ssex,sage,sdept) 课程表 course(cno,cname,cpno,ccredit) 学生选课 sc(sno,cno,grade) 二 设定主码

1 student表的主码:sno2 course表的主码:cno 3 sc表的主码:sno,cno 1写出使用 create table 语句创建表 student , sc, course 的sql语句 2

3 删除student表中的元组

4在数据库school中删除关系student

5在student表添加属性sbirthdate 类型 datetime delete

1 删除所有 jsj 系的男生 delete from student where sdept=’jsj’ and ssex=’男’; 2 删除“数据库原理”的课的选课纪录

delete from sc where cno in (select cno fromcourse where cname=’数据库原理’); update

1 修改 0001 学生的系科为: jsj 2 把陈小明的年龄加1岁,性别改为女。 2 修改李文庆的1001课程的成绩为 93 分 3 把“数据库原理”课的成绩减去1分 select 查询语句 一 单表

1查询年龄在19至21岁之间的女生的学号,姓名,年龄,按年龄从大到小排列。 2查询姓名中第2个字为“明”字的学生学号、性别。 3查询 1001课程没有成绩的学生学号、课程号

4查询jsj 、sx、wl 系的年龄大于25岁的学生学号,姓名,结果按系排列 5按10分制查询学生的sno,cno,10分制成绩

(1-10分 为1 ,11-20分为2 ,30-39分为3,。。。90-100为10) 6查询 student 表中的学生共分布在那几个系中。(distinct) 7查询0001号学生1001,1002课程的成绩。 二 统计

1查询姓名中有“明”字的学生人数。 2计算‘jsj’系的平均年龄及最大年龄。 3查询学生中姓名为张明、赵英的人数

4计算每一门课的总分、平均分,最高分、最低分,按平均分由高到低排列 5 计算 1001,1002 课程的平均分。

6 查询平均分大于80分的学生学号及平均分 7 统计选修课程超过 2 门的学生学号 8 统计有10位成绩大于85分以上的课程号。 9 统计平均分不及格的学生学号 10 统计有大于两门课不及格的学生学号 三 连接

1查询 jsj 系的学生选修的课程号

2查询选修1002 课程的学生的学生姓名 (不用嵌套及嵌套2种方法) 3查询数据库原理不及格的学生学号及成绩

4查询选修“数据库原理”课且成绩 80 以上的学生姓名(不用嵌套及嵌套2种方法) 5查询平均分不及格的学生的学号,姓名,平均分。 6查询女学生平均分高于75分的学生姓名。 7查询男学生学号、姓名、课程号、成绩。(一门课程也没有选修的男学生也要列出,不

四 嵌套、相关及其他

1 查询平均分不及格的学生人数

2 查询没有选修1002 课程的学生的学生姓名

3 查询平均分最高的学生学号及平均分 (2种方法 top , any , all) *4 查询没有选修1001,1002课程的学生姓名。

5 查询1002课程第一名的学生学号(2种方法) 6 查询平均分前三名的学生学号 7 查询 jsj 系的学生与年龄不大于19岁的学生的差集 8 查询1001号课程大于90分的学生学号、姓名及平均分大于85分的学生学号、姓名 9 查询每门课程成绩都高于该门课程平均分的学生学号 10 查询大于本系科平均年龄的学生姓名

答案 参考答案

1 create table student (snochar(6), sname varchar(8), ssexchar(2), sagesmallint, sdept varchar(15), primary key(sno)); create table sc

(snochar(6), cno char(4),

grade decimal(12,2), primary key(sno,cno)); insert into student

values( ’4001’,’赵茵’,’男’,20,’sx’) delete from student drop table student

alter table student add sbirthdate datetime 1 select sno, sname, sage from student

where ssex=’女’ and sage between 19 and 21order by sage desc; 2 select sno, ssexfrom student

where sname like ’_明% ’ ; 3 select sno, cnofrom sc

where grade is null and cno=’1001’ ; 4 select sno, sname from student where sdept in (’jsj’,’sx’,’wl’) and sage>25 group by sdept; select sno, cno, grade/10.0+1 as levelfrom sc ;

select distinct sdept from student ; select grade from sc where sno=’0001’ and (cno=’1001’ or cno=’1002’) ;

select count(*) from student where sname like ’%明% ’ ; select avg(sage),max(sage) from student where sdept=’jsj’ ; select cno,sum(grade),avg(grade),max(grade),min(grade) from sc group by cno order by avg(grade) desc ;

select cno, avg(grade) from sc where cno in(‘1001’,’1002’) group by cno ; select sc.sno ,avg(grade) from scgroup by sc.sno having avg(grade)>80 ;

select sno from sc group by sno having count(*)>2 ;

select cno from sc where grade>85 group by cno having count(*)=10 ; select sno from sc group by sno having avg(grade)<60 ;

select sno from sc where grade<60 group by sno having count(*)>2 ; select cno from student,sc where student.sno=sc.sno and sdept=’jsj’ ; a:select

sname from student,sc where student.sno=sc.sno and cno=’1002’

b:select sname from student where sno in (select sno from sc where cno=’1002’) select sno,grade from sc,course where sc.cno=course.cno and cname=’数据库原理’ and grade <60 a:select sname from student ,sc,course

where student.sno=sc.sno and sc.cno=course.cno and grade>80 and cname=’ 数据库原理’ b:select sname from student where sno in (select sno from sc where grade>80 and cno in (select cno from course where cname=’ 数据库原理’)) select sno,sname,avg(grade) from sc,studentwhere student.sno=sc.snogroup by student.sno having avg(grade)<60 a:select sname from student where ssex=’女’ and sno in(select sno from sc group by sno having avg(grade)>75)

b:select sname from sc,student where student.sno=sc.sno and ssex=’女’group by student.sno having avg(grade)>75

select student.sno,sname,cno,grade from student left join sc on student.sno=sc.sno and ssex=’男’

select count(*) from student where sno in( select sno from sc group by sno havingavg(grade)<60) select sname from student where sno not in(select sno from sc where cno=’1002’) student

0001 aax 0002 bb 0003 ccx sc

0001 1001 0001 1002 0002 1001 0003 1002

select sname from student where not exists(select* from sc where cno=’1002’ and sc.sno=student.sno)

a:select top 1 sno,avg(grade) from sc group by sno order by avg(grade) desc b:select sno, avg(grade) from sc group by sno having avg(grade)=(select top 1 avg(grade) from scgroup by sno order by avg(grade) desc) c:select sno, avg(grade) from sc group by sno

having avg(grade)>=all ( select avg(grade) from sc group by sno) select sname from student where not exists(

select * from course where cno in(‘1001’,’1002’) and not exists(select * from sc where sno =student.sno and cno=course.cno) ) a:select top 1 sno from sc cno=’1002’ order by grade desc b:select sno from sc where cno=’1002’ and grade >=all (篇二:sql语句强化练习题及答案 sql语句强化练习题及答案 一、简单查询

1、列出全部学生的信息。 select * from 学生

2、列出软件专业全部学生的学号及姓名。

select 学号,姓名 from 学生 where 专业=软件 3、列出所有必修课的课号。

select distinct 课号 from 必修课

4、求1号课成绩大于80分的学生的学号及成绩,并按成绩由高到低列出。

select 学号,成绩 from 选课 where 课号=1 and 成绩>80 order by 成绩 desc 5、列出非软件专业学生的名单。

方法一:select 姓名 from 学生 where 专业<>软件 方法二:select 姓名 from 学生 where not 专业=软件 方法三:select 姓名 from 学生 where 专业!=软件 6、查询成绩在70~80分之间的学生选课得分情况

方法一:select * from 选课 where 成绩>=70 and 成绩<=80 方法二:select * from 选课 where 成绩 between 70 and 80 不在此范围内的查询:(注意写出和以下语句等价的语句) select * from 选课 where 成绩 not between 70 and 80 7、列出选修1号课或3号课的全体学生的学号和成绩。

方法一:select 学号,成绩 from 选课 where 课号=1 or 课号=3 方法二:select 学号,成绩 from 选课 where 课号 in (1,3)

相反条件查询:select 学号,成绩 from 选课 where 课号 not in (1,3) 8、列出所有98级学生的学生成绩情况。 select * from 选课 where 学号 like 98%

select * from 选课 where 学号 like 98_ _ _ _

相反条件查询:select * from 选课 where 学号 not like 98% 9、列出成绩为空值(或不为空值)的学生的学号和课号。 答案一:select 学号,课号 from 选课 where 成绩 is null 答案二:select 学号,课号 from 选课 where 成绩 is not null 10、求出所有学生的总成绩。

select sum(成绩) as 总成绩 from 选课 11、列出每个学生的平均成绩。

select 学号,avg(成绩) as 平均成绩 from 选课 group by 学号 12、列出各科的平均成绩、最高成绩、最低成绩和选课人数。 select 课号,avg(成绩) as 平均成绩,max(成绩) as 最高分,;

min(成绩) as 最低分,count(学号) as 选课人数 from 选课 group by 课号 二、连接查询 (一)简单连接

1、列出选修1号课的学生姓名及成绩。

select 姓名,成绩 from 学生,选课 where 学生.学号=选课.学号 and 课号=1 2、列出选修1号课的学生的学号、姓名及成绩。

select 学生.学号,姓名,成绩 from 学生 s,选课 x where s.学号=x.学号 and 课号=1 3、求出总分大于150的学生的学号、姓名及总成绩。

select 学生.学号,姓名,sum(成绩) as 总成绩 from 学生,选课;

where 学生.学号=选课.学号 group by 选课.学号 having sum(成绩)>150 (二)自连接查询

1、列出那些专业相同的学生相应的姓名及专业信息。 select a.姓名,b.姓名,专业 from 学生 a,学生 b where a.学号<>b.学号 and a.专业=b.专业

2、求至少选修1号课和2号课的学生的学号。

select x.学号 from 选课 x,选课 y where x.学号=y.学号 and x.课号=1 and y.课号=2

3、有以下表rate.dbf

币种1代码 c(2)、币种2代码 c(2)、买入价 n(8,4)、卖出价 n(8,4) 外汇汇率.dbf

币种1 c(4)、币种2 c(4)、买入价 n(8,4)、卖出价 n(8,4) 外汇代码.dbf

外汇名称 c(10)、外汇代码 c(10)

要求:将所有“外汇汇率”表中的数据插入rate表中并且顺序不变,由于“外汇汇率”中的币种1和币种2存放的是外币名称,而rate表中的币种1代码和币种2代码应该存放外币代码,所以插入时要做相应的改动,外币名称与外向代码的对应关系存储在“外汇代码”表中。

select a.外币代码 as 币种1代码,b.外币代码 as 币种2代码,; 买入价,卖出价 from 外汇代码 a,外汇汇率,外汇代码 b;

where a.外币名称=外汇汇率.币种1 and b.外币名称=外汇汇率.币种2 into table rate 4、假定有“雇员”表(雇员号 c(2),雇员姓名 c(6),经理号 c(2)),根据雇员关系列出上一级经理及其所领导的职员清单。(教案中的例题)

select 领导,s.雇员姓名,雇员,e.雇员姓名 from 雇员 s,雇员 e where s.雇员号=e.经理

(三)超连接

1、列出选修1号课的学生姓名及成绩。 方法一:(使用简单连接查询格式)

select 姓名,成绩 from 学生,选课 where 学生.学号=选课.学号 and 课号=1 方法二:(使用内部连接格式)

select 姓名,成绩 from 学生 inner join 选课 on 学生.学号=选课.学号 where 课号=1 方法三:内部连接的inner短语可以省略。(与方法二等价)

select 姓名,成绩 from 学生 join 选课 on 学生.学号=选课.学号 where 课号=1 2、查询订货管理数据库中数据的仓库号、城市、供应商名和地址信息。 方法一:使用简单连接格式。

select 仓库.仓库号,城市,供应商名,地址 from 供应商,订购单,职工,仓库; where 供应商.供应商号=订购单.供应商号 and订购单.职工号=职工.职工号 ; and 职工.仓库号=仓库.仓库号

方法二:使用超连接的内部连接格式。(注意连接条件的顺序)

select 仓库.仓库号,城市,供应商名,地址 from 供应商 join 订购单 join 职工 join 仓库 ;

on 职工.仓库号=仓库.仓库号 on 订购单.职工号=职工.职工号 on 供应商.供应商号=订购单.供应商号

3、查询没有选修任何课程的学生姓名。 方法一:使用嵌套查询

select 姓名 from 学生 where 学号 not in (select 学号 from 选课) 方法二:使用超连接的右连接。

select 姓名 from 选课 right join 学生 on 选课.学号=学生.学号 where 选课.学号<>学生.学号

方法三:使用超连接的左连接。(注意表名顺序和方法二的不同)

select 姓名 from 学生 left join 选课 on 选课.学号=学生.学号 where 选课.学号<>学生.学号

本文来源:https://www.bwwdw.com/article/41ff.html

Top