实验六:表的查询

一.实验目的

    掌握并理解带有EXISTS谓词的子查询

二.实验设备

    PC机一台

三.实验内容

    练习带有EXISTS谓词的子查询对表进行查询

四.实验步骤

打开SQL Server Management Studio 18,弹出一个连接到服务器的窗口,在这里面,需要将服务器名称改为(local),点击连接。

/*查询所有选修了1号课程的学生姓名*/
SELECT Sname
FROM Student 
WHERE EXISTS
  (SELECT*
  FROM SC
  WHERE Sno=Student.Sno AND Cno='1');

/*查询没有选修1号课程的学生姓名*/
SELECT Sname
FROM Student 
WHERE NOT EXISTS
  (SELECT*
  FROM SC
  WHERE Sno=Student.Sno AND Cno='1');

SELECT Sno,Sname,Sdept 
FROM Student S1
WHERE EXISTS
  (SELECT*
   FROM Student S2
   WHERE S2.sdept=S1.Sdept AND
         S2.Sname='刘晨');

/*查询选修了全部课程的学生姓名*/
SELECT Sname
FROM Student 
WHERE NOT EXISTS
  (SELECT*
   FROM Course
   WHERE NOT EXISTS
     (SELECT*
      FROM SC
WHERE Sno=Student.Sno
AND Cno =Course .Cno ));

/*查询至少选修了学生201215122选修的全部课程的学生号码*/
SELECT DISTINCT Sno
FROM SC SCX
 WHERE NOT EXISTS
 (SELECT*
  FROM SC SCY
  WHERE SCY .Sno='201215122' AND
  NOT EXISTS
  (SELECT*
  FROM SC SCZ
  WHERE SCZ .sno =SCX .Sno AND
  SCZ .Cno=SCY .Cno));

实验六:表的查询

实验六:表的查询

 

 

 

上一篇:MySQL常用操作、插入函数


下一篇:MySQL 数据库实验二