前言
战神的课实例好多太棒了,推荐,本节关于SQL复杂查询
正文
首先可以根据课程ppt上的图文建立一个SCT数据库,老师上课的内容大部分都用的是这个数据库中的实例,除了手写,可以实际操作敲一敲巩固记忆。
一、(NOT)in子查询
示例:列出张三、王三同学的所有信息
Select * From Student
Where Sname in (“张三”, “王三”);
//此处直接使用了某一子查询的结果集合。如果该集合是已知的固定的,可以如上直接书写
上述示例相当于
Select * From Student
Where Sname = “张三” or Sname = “王三”;
1. 题目
示例1:列出选修了001号课程的学生的学号和姓名
示例2:求既学过001号课程, 又学过002号课程的学生的学号(重要)
示例3:列出没学过李明老师讲授课程的所有同学的姓名?
2. 相关子查询:
- 内层查询需要依靠外层查询的某些参量作为限定条件
才能进行的子查询 - 外层向内层传递的参量需要使用外层的表名或表别名来限定
3.非相关子查询
- 内层查询独立进行,没有涉及任何外层查询相关信息的子查询
- 前面的子查询示例都是非相关子查询
4.答案
示例1
Select S#, Sname From Student
Where S# in ( Select S# From SC Where C# = ‘001’ ) ;
示例2
Select S# From SC
Where C# = ‘001’ and
S# in ( Select S# From SC Where C# = ‘002’ ) ;
示例3
Select Sname From Student
Where S# not in ( Select S# From SC, Course C, Teacher T
Where T.Tname = ‘李明’ and SC.C# = C.C#
and T.T# = C.T# );
二、θ some / θ all子查询
语法中, θ 是比较运算符: < , > , >= , <= , = , <>。
- 如果表达式的值至少与子查询结果的某一个值相比较满足θ 关系,则“表达式 θ some (子查询)”的结果便为真;
- 如果表达式的值与子查询结果的所有值相比较都满足θ 关系,则“表达式 θ all (子查询)”的结果便为真;
示例1:找出工资最低的教师姓名
Select Tname From Teacher
Where Salary <= all ( Select Salary From Teacher );
示例2:找出001号课成绩不是最高的所有学生的学号
Select S# From SC
Where C# = “001” and
Score < some ( Select Score From SC Where C# = “001” );
示例3:找出所有课程都不及格的学生姓名(相关子查询)
Select Sname From Student
Where 60 > all ( Select Score From SC
Where S# = Student.S# );
- 题目
题目1:找出001号课成绩最高的所有学生的学号
题目2:找出98030101号同学成绩最低的课程号
题目3:找出张三同学成绩最低的课程号
- 等价变换
如下两种表达方式含义是相同的
- 表达式 = some (子查询)
- 表达式 in (子查询)
示例:
Select Sname From Student S
Where S# in ( Select S# From SC
Where S# = S.S# and C# = ‘001’ ) ;
Select Sname From Student S
Where S# = some ( Select S# From SC
Where S# = S.S# and C# = ‘001’ ) ;
如下两种表达方式含义却是不同的,请注意
- 表达式 not in (子查询)
- 表达式 <> some(子查询)
与notin等价的是
- 表达式 <> all (子查询)
示例:
Select Sname From Student S
Where S# not in ( Select S# From SC
Where S# = S.S# and C# = ‘001’ ) ;
Select Sname From Student S
Where S# <>some ( Select S# From SC
Where S# = S.S# and C# = ‘001’ ) ;
Select Sname From Student S
Where S# <> all ( Select S# From SC
Where S# = S.S# and C# = ‘001’ ) ;
- 答案
题目1
Select S# From SC
Where C# = “001” and
Score >= all ( Select Score From SC Where C# = “001” );
题目2
Select C# From SC
Where S# = “98030101” and
Score <= all ( Select Score From SC Where S# = “98030101” );
题目3
Select C# From SC, Student S
Where Sname = “张三” and S.S#=SC.S# and
Score <= all ( Select Score From SC
Where S#=S.S# );
三、(NOT) EXISTS子查询
[not] Exists [not] Exists(子查询)
子查询结果中有无元组存在
示例:检索选修了赵三老师主讲课程的所有同学的姓名
Select DISTINCT Sname From Student
Where exists ( Select * From SC, Course, Teacher
Where SC.C# = Course.C# and SC. S# = Student.S#
and Course.T# = Teacher.T# and Tname = ‘赵三’ ) ;
不加not形式的Exists谓词可以不用,比如上面例子就可以直接写成:
Select DISTINCT Sname From Student, SC, Course, Teacher
Where SC.C# = Course.C# and SC.S# = Student.S#
and Course.T# = Teacher.T# and Tname = ‘赵三’ ) ;
然而notExists却可以实现很多新功能
示例:检索学过001号教师主讲的所有课程的所有同学的姓名
Select Sname From Student
Where not exists //不存在
( Select * From Course //有一门001教师主讲课程
Where Course.T# = ‘001’ and not exists //该同学没学过
( Select * From SC
Where S# = Student.S# and C# = Course.C# ) );
上述语句的意思:不存在有一门001号教师主讲的课程该同学没学过
思考:利用元组演算 怎样表达?,利用关系代数怎样表达?
关系代数:Πsname(Student ⋈ Π c# SC ÷ Π c#( σ t#=‘001’ Course))
关系演算:{t[sname]|t∈Student ∧∀(u∈Course ∧u[T#]=‘001’)(∃(w∈sc)(u[c#]=w[c#]∧t[s#]=w[s#]))}
1. 题目
题目1:列出没学过李明老师讲授任何一门课程的所有同学的姓名
题目2:列出至少学过98030101号同学学过所有课程的同学的学号(利用元组演算怎样表达?,利用关系代数怎样表达?)
题目3:已知SPJ(Sno, Pno, Jno, Qty), 其中Sno供应商号,Pno零件号,Jno工程号,Qty数量,列出至少用了供应商S1供应的全部零件的工程号
2. 答案
题目1
Select Sname From Student
Where not exists //不存在
( Select * From Course, SC, Teacher //学过一门课程
Where Tname=‘李明’ and Course.T# =Teacher.T#
and Course.C# = SC.C# and S# = Student.S# );
题目2
Select DISTINC S# From SC SC1
Where not exists //不存在
( Select * From SC SC2 //有一门课程
Where SC2.S# = ‘98030101’ and not exists //该同学没学过
( Select * From SC
Where C# = SC2.C# and S# = SC1.S# ) );
关系代数与元祖演算
题目3
Select DISTINCT Jno From SPJ SPJ1
Where not exists //不存在
( Select * From SPJ SPJ2 //有一种S1的零件
Where SPJ2.Sno = ‘S1’ and not exists //该工程没用过
( Select * From SPJ SPJ3
Where SPJ3.Pno = SPJ2.Pno
and SPJ3.Jno = SPJ1.Jno ) );