实验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));

数据库实验5 数据库的嵌套查询实验相关推荐

  1. MySql实验嵌套查询_实验五 数据库的嵌套查询实验

    实验五数据库的嵌套查询实验 本实验需要2学时. 一.实验目的 使学生进一步掌握SQL Server或oracle的企业管理器的使用方法,加深SQL 语言的嵌套查询语句的理解. 二.实验内容 在SQL ...

  2. 数据库实验之《表的连接和嵌套查询》

    表的连接&嵌套查询 实验目的 熟练掌握Mysql查询方法,加深对SQL语言查询语句的理解. 掌握多表的连接查询与嵌套查询语句的正确写法和实验操作方法. 实验环境 Windows10,MySQL ...

  3. 数据库实验:数据库高级查询

    数据库实验:数据库高级查询 实验过程 (1) IN 嵌套查询 (2) 单层EXISTS 嵌套查询 (3) FROM 子句中的嵌套查询 (4) 集合查询(交) (5) 集合查询(并) (6) 集合查询( ...

  4. MySQL实验四数据库的查询_MySQL数据库查询(实验四)

    MySQL数据库查询 准备工作:脚本文件xkgl.sql下载:xkgl脚本.sql 1.执行脚本xkgl.sql (创建xkgl库.表及插入数据),观察有无错误,如有记录错误信息,并解决. (1) 执 ...

  5. sql镶嵌查询_sql数据库的嵌套查询

    实验四:数据库的嵌套查询实验 学号: 姓名: 实验四:数据库的嵌套查询实验 实验目的: 加深对嵌套查询语句的理解. 实验内容: 使用 IN . 比较符. ANY 或 ALL 和 EXISTS 操作符进 ...

  6. 数据库系统实验4:SQL——SELECT查询操作

    数据库系统实验4:SQL语言--SELECT查询操作 数据库系统实验4:SQL语言--SELECT查询操作 实验环境 实验内容 步骤及过程 首先按以下SQL语句创建测试用的jxgl数据库 使用SQL语 ...

  7. 数据库实验三 嵌套查询和视图操作

    实验三 嵌套查询和视图操作 实验目的: 1.  通过本实验能够熟练应用sql语言使用IN.比较符.ANY或ALL和EXISTS操作符进行嵌套查询操作. 2.  掌握视图的定义.查询.修改. 实验要求: ...

  8. MySQL实验四数据库的查询_MySQL数据库实验四:嵌套查询

    实验四          嵌套查询 一.实验目的 掌握SELECT语句的嵌套使用,实现表的复杂查询,进一步理解SELECT语句的高级使用方法. 二.实验环境 三.实验示例 1.  查询与"刘 ...

  9. SQL server数据库实验(三)数据库的嵌套查询和集合查询

    文章目录 一.针对教学管理数据库SCT,进行以下各种嵌套查询与集合查询 1.查询选修了"数据库"课程的学生信息 2.查询与学生"李维"在同一个院系的学生选课信息 ...

最新文章

  1. putty修改字体配色
  2. block才会执行 mono_C-BLOCK录制《我要上春晚》,目测会上湖南分会场
  3. 用权值实现数据被抽取的概率
  4. 深度学习在医学影像的三大类项目应用
  5. WordPress免费主题CorePress v4.9
  6. Java 并发编程CyclicBarrier的应用与源码解析(基于ReentrantLock实现)
  7. mysql 5.7和8.0区别_SpringBoot 2.0 教程实战 MySQL 读写分离
  8. excel vlookup多个条件匹配多列_Excel教程第12课:VLOOKUP函数近似匹配到底怎么回事,原理+操作...
  9. python 拼音相似度_用Python进行简单的文本相似度分析
  10. 信号跟单时提示mt4与服务器断开,MT4平台操作中遇到的一些常见问题和解决方法 -...
  11. 电路串联和并联图解_串联电路和并联电路
  12. 金蝶软件常用基础SQL数据表
  13. 用Python寻找最优投资组合
  14. excel批量删除所有空白行
  15. 源码再现,SpringBoot 居然只有一个 IOC 容器
  16. c语言版计算坐标方位角,直线坐标计算程序
  17. 京东淘宝手机销售量排行
  18. win7计算机自动关机设置在哪里设置方法,win7自动关机怎么设置?W7自动关机命令设置方法...
  19. 索爱SA-K37拉杆音箱,随时随地帮你撑起一个大舞台
  20. 【一 DE1-SOC】quartus II下载程序步骤

热门文章

  1. 2018年python工作好找吗-2018年最火的5大Python开源项目,总有适合你的!
  2. python与excel-超简单:用Python让Excel飞起来
  3. 如何系统的自学python-作为小白,如何系统的自学PythonWeb开发?
  4. python免费课程400节-北京市python儿童学编程
  5. python基本代码教程-(Python基础教程之三)Python代码中添加注释
  6. python练手经典100例-Python 的练手项目有哪些值得推荐?
  7. 最专注和高效的查词法?网易有道词典笔2.0评测
  8. python爬虫中for循环无法每一段输出_Python入门到掌握只需要这3大,4类,5大,6种,即可,附教程...
  9. 从输入url到页面加载完成发生了什么
  10. Vue的调试工具(Chrome 浏览器)配置