数据库实验5 数据库的嵌套查询实验

实验5 数据库的嵌套查询实验

  • 5.1实验目的及要求

    加深对嵌套查询语句的理解

  • 5.2实验内容

使用IN、比较符、ANY或ALL和EXIST操作符进行嵌套查询操作

  • 5.3实验步骤

5.3.1使用带IN谓词的子查询
1.查询与‘孙悟空’在同一个系学习的学生信息;
Select * from student where sdept in
(select sdept from student where sname=’孙悟空’;
在这里插入图片描述

比较: Select * from student where sdept =
(select sdept from student where sname=’孙悟空’);
在这里插入图片描述

比较: Select * from student where sdept =
(select sdept from student where sname=’孙悟空’)and sname<>’孙悟空’;
在这里插入图片描述

2.查询选修了课程名为‘信息系统’的学生的学号和姓名:
在mysql中:select sno,sname from student where sno in
(select sno from sc where cno in
(select cno from course where cname=’信息系统’));
在这里插入图片描述

3.查询选修了课程‘1’和课程‘2’的学生的学号:
Select sno from student where sno in(select sno from sc where cno=’1’)
and sno in(select sno from sc where cno=’2’);
在这里插入图片描述
比较:查询选修了课程‘1’或课程‘2’的学生的sno;
Select sno from sc where cno=’1’or cno=’2’;
在这里插入图片描述

比较连接查询:
Select A.sno from sc A,sc Bwhere A.sno=B.sno and A.cno=’1’and B.cno=’2’;
在这里插入图片描述

5.3.2使用带比较运算的子查询
4.查询比‘孙悟空’年龄小的所有学生的信息:
Select * from student where sage<
(select sage from student where sname=’孙悟空’);
在这里插入图片描述

5.3.3使用带Any,All谓词的子查询
5.查询比其他系中软件工程系某一学生年龄小的学生的姓名和年龄;
Select snmae,sage from student where sage<Any
(select sage from student where sdept=’软件工程’)
And sdept<>’软件工程’;
在这里插入图片描述

6.查询其他系中比软件工程系学生年龄都小的学生姓名和年龄;
select sname,sage from student were sage<ALL
(select sage from student where sdept=’软件工程’)
And sdept<>’软件工程’;
在这里插入图片描述

7.查询与网络工程系所有学生的年龄均不同的学生的学号,姓名和年龄:
Select sno,sname,sage from student where sage<>all
(slect sage from student where sdept=’网络工程’);
在这里插入图片描述

5.3.4使用带Exists谓词的子查询和相关子查询
8.查询与其他所有学生年龄均不同的学生学号,姓名和年龄:
Select sno,sname,sage from student A where not exists
(select * from student B where A.sage=B.sage and A,sn0<>B.sno);
在这里插入图片描述

9.查询所有选修了1号课程的学生的姓名:
Select sname from student where exists
Select sname from student where sno =student.sno and cno=’1’);
在这里插入图片描述

10.查询没有选修了1号课程的学生姓名:
Select sname from student where not exists
(select * from sc where sno=student.sno and cno=’1’)
在这里插入图片描述

11.查询选修了全部课程的学生姓名:
Select sname from student where not exists
(select * from course where not exists
(select * from sc where sno=A.sno and cno=B.cno));
在这里插入图片描述

12.查询至少选修了学生12002选修的全部课程的学生的学号:
Select distinct sno from sc A where not exists
(select * from sc B where sno=’12002’and not exists
(select * from sc C where sno =A.sno and cno=B.cno));
在这里插入图片描述

13.求没有人选修的课程号cno 和cname;
Select cno,cname from course C where not exists
(select * from sc where sc.cno=C.cno;
在这里插入图片描述

14.查询满足条件的(sno,cno)对,其中该学号的学生没有选修该课程号cno的课程
Select sno,cno from student,course where not exists
(select * from sc where cno=course.cno and sno=student.sno);
在这里插入图片描述

15.查询每个学生的课程成绩最高的成绩信息(sno,cno,grade);
Select * from sc A where grsde=
(select max(grsde)from sc where sno=A.sno);
在这里插入图片描述

思考:
如何查询所有学生都选修了的课程的课程号cno?
Select cno from course where not exists (select * from course where not exists(select * from sc where sno=student.sno and cno=course.cno));

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面为你介绍一下SQL数据库实验嵌套查询嵌套查询是指在一个查询语句中嵌入另一个查询语句。嵌套查询可以通过子查询来实现,子查询可以嵌套在SELECT、FROM、WHERE等子句中,用于获取需要的数据。 下面是一个简单的示例: 假设我们有两张表:学生表(student)和成绩表(score),它们的结构如下: 学生表(student): | id | name | age | gender | |----|--------|-----|--------| | 1 | 张三 | 18 | 男 | | 2 | 李四 | 19 | 女 | | 3 | 王五 | 20 | 男 | | 4 | 赵六 | 21 | 女 | 成绩表(score): | id | student_id | course | score | |----|------------|--------|-------| | 1 | 1 | 语文 | 80 | | 2 | 1 | 数学 | 90 | | 3 | 2 | 语文 | 85 | | 4 | 2 | 数学 | 95 | | 5 | 3 | 语文 | 70 | | 6 | 3 | 数学 | 80 | 现在,我们要查询出每个学生的总成绩和平均成绩,可以使用嵌套查询来实现: ``` SELECT s.id, s.name, (SELECT SUM(score) FROM score WHERE student_id = s.id) AS total_score, (SELECT AVG(score) FROM score WHERE student_id = s.id) AS avg_score FROM student s; ``` 上述SQL语句中,首先查询学生表(student),然后通过嵌套查询获取每个学生的总成绩和平均成绩。在子查询中,WHERE子句指定了查询条件,即只查询与当前学生相关的成绩信息。 嵌套查询可以实现非常复杂的查询,但也可能导致查询效率低下。因此,在使用嵌套查询时,需要根据实际情况合理设计查询语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值