实验4数据查询答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

4_:DML的数据查询

简单查询

1)查询全体学生的详细记录;Select * From student

2)查询所有选修过课的学生的学号;Select distinct sno from sc

3)查询考试成绩不及格的学生的学号;Select sno from sc where score<60

4)查信息系(IS)、数学系(MA)和计算机科学系(CS)的学生的姓名和性别;

Select sname,ssex from student w here sdept in (‘IS’,’MA’,’CS’)

5)查所有姓刘的学生的姓名、学号和性别;

Select sname,sno,sse x from student w here sname like ‘刘%’

6)查课程名含有DB_Design 课程的课程号和学分;

Select cname,credit from course where cname like ‘%DB_Design%’

7)查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列;

Select sno,score from sc where cno=’3’ order by score

8)查询学习1号课程的学生最高分数;Select max(score) from sc where cno=’1’

连接查询:

1)查询各个课程号与相应的选课人数;Select cno,count(sno) from sc group by cno

2)查询每个学生及其选修课程的情况;

Select student.*,sc.* from student,sc where student.sno=sc.sno

3)查询选修2号课程且成绩在90分以上的所有学生;Select student.sno,sname,o,sc.score from st udent,sc where student.sno=sc.sno and cno=’2’ and score>=90

4)查询每个学生及其选修的课程名及其成绩;

Select student.*,ame,sc.score from student,sc,course where student.sno=sc.sno and o=o

5)查询每一门课的间接先行课(即先行课的先行课)。

Select o,second.cpno from couse first,course second where first.cpno=o

嵌套查询:

(1)求选修了高等数学的学生学号和姓名。

Select sno,sname from st udent where sno in

(select sno from sc where cno in

(select cno from course where cname=’高等数学’))

(2)求2课程的成绩高于张三的学生学号和成绩。

Select sno,score from sc where cno=’2’ and score >(select score from sc where cno=’2’ and sno=(select sno from student w here sname=’张三’))

(3)求其他系中年龄小于计算机系年龄最大者的学生。

Select * from student w here sdept<>’CS’ and sage<(select max(sage) from student where adept=’CS’)

(4)求其他系中比计算机系学生年龄都小的学生。

Select * from student w here sdept<>’CS’ and sage< all (select sage from student where adept=’CS’)

(5)求选修了2号课程的学生姓名。Select sname from student where sno in (select distinct sno from sc where cno=’2’)

(6)求没有选修2课程的学生姓名。Select sname from student where sno not in (select distinct sno from sc where cno=’2’)

1.用SELECT语句对表进行嵌套查询操作,一个Select…From…Where语句称为一个查询块,将一个查询块嵌套在另一个查询块的Where子句或Having短语的条件中的查询,就是嵌套查询。主要有使用In操作符、使用比较运算符的嵌套查询方式。

2.用SELECT语句对表进行组合查询操作,将SELECT语句的查询结果集再进行集合运算就构成了SQ L的组合查询。

1

相关文档
最新文档