sql练习题+答案(3)

2019-09-02 14:56

')

(41) 查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。分别用

ALL谓词和集函数

----用ALL select sname,age from student where age

(42) 查询所有选修了1号课程的学生姓名。(分别用嵌套查询和连查询)

----嵌套查询 select sname from student where sno in (select sno from cs where cno=1) ----连接查询 select sname from student,cs where student.sno=cs.sno and cs.cno=1

(43) 查询没有选修1号课程的学生姓名。

select sname from student where sno in (select sno from cs where cno!=1)

(44) 查询选修了全部课程的学生姓名。

select sname from student where not exists (select * from course where not exists (select * from cs where cs.sno=student.sno and cs.cno=course.cno))

(45) 查询至少选修了学生95002选修的全部课程的学生号码。

select distinct sno from sc scx where not exists (select * from cs scy where scy.sno='95002' and not exists (select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno))

(46) 查询计算机科学系的学生及年龄不大于19岁的学生的信息。

select * from student where dept='计算机科学系' or age<19

(47) 查询选修了课程1或者选修了课程2的学生的信息。

select student.* from student,cs where student.sno = cs.sno and (cs.cno=1 or cs.cno=2)

(48) 查询计算机科学系中年龄不大于19岁的学生的信息。

select * from student where age<=19 and dept='计算机科学系'

(49) 查询既选修了课程1又选修了课程2的学生的信息。

select * from student where sno in( select sno from cs where cno='003' and sno in( select sno from cs where cno='004' )) ----用exists查询 select * from student where exists ( select * from cs where student.sno=cs.sno and cno='003' and sno in( select sno from cs where cno='004' ))

(50) 查询计算机科学系的学生与年龄不大于19岁的学生的差集。

select *from student where dept='计算机科学系' and age>19

(51) 通过查询求学号为1学生的总分和平均分。

select sum(cj) as '总分',avg(cj)'平均分' from cs where sno=1

(52) 求出每个系的学生数量

select dept,count(sno) as '学生个数' from student group by dept

(53) 查询平均成绩大于85的学生学号及平均成绩。

select sno,avg(cj) from cs group by sno having avg(cj)>85

(54) 要求查寻学生的所有信息,并且查询的信息按照年龄由高到低排序,如果年龄相等,则按照学号从低到高排序

select * from student order by age desc,sno asc

1.在SELECT语句中DISTINCT、ORDER BY、GROUP BY和HAVING子句的功能各是什么?

答 各子句的功能如下。

DISTINCT:查询唯一结果。

ORDER BY:使查询结果有序显示。 GROUP BY:对查询结果进行分组。 HAVING:筛选分组结果。

2.在一个SELECT语句中,当WHERE子句、GROUP BY子句和HAVING子句同时出现在一个查询中时,SQL的执行顺序如何?

答 其执行顺序如下:

(1)执行WHERE子句,从表中选取行。 (2)由GROUP BY对选取的行进行分组。 (3)执行聚合函数。

(4)执行HAVING子句选取满足条件的分组。


sql练习题+答案(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:北部新区H29地块项目可行性研究报告 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: