# SQL数据查询语言(DQL)之子查询
SQL数据查询语言(DQL)之子查询
一、含义
嵌套在其他语句内部的select语句称为子查询或内查询,
外面的语句可以是insert、update、delete、select等,一般select作为外面语句较多
外面如果为select语句,则此语句称为外查询或主查询
二、分类
1、按出现位置😵
select后面:
仅仅支持标量子查询
from后面:
表子查询
where或having后面:
标量子查询
列子查询
行子查询
exists后面:
标量子查询
列子查询
行子查询
表子查询
2、按结果集的行列😄
标量子查询(单行子查询):结果集为一行一列
列子查询(多行子查询):结果集为多行一列
行子查询:结果集为多行多列
表子查询:结果集为多行多列
三、示例
where或having后面
1、标量子查询
案例:查询最低工资的员工姓名和工资
①最低工资
select min(salary) from employees
②查询员工的姓名和工资,要求工资=①
select last_name,salary
from employees
where salary=(
select min(salary) from employees
);
2、列子查询
案例:查询所有是领导的员工姓名
①查询所有员工的 manager_id
select manager_id
from employees
②查询姓名,employee_id属于①列表的一个
select last_name
from employees
where employee_id in(
select manager_id
from employees
);
四、练习
# 1、查询Score表中的最高分的学生学号和课程号。√
select *
from score;
## 最高分
select max(Degree) 最高分
from score;
### 学生学号和课程号 要求成绩等于最高分
select Sno 学生学号, Cno 课程号
from score
where Degree = (
select max(Degree) 最高分
from score
);
####为什么我总是要把简单的问题复杂化呢?
select SNO, CNO
from Score
where Degree = (
select MAX(Degree)
from Score
);
# 2、查询所有选修“计算机导论”课程的“男”同学的成绩表
select *
from course;
## 查询成绩表
select Degree 成绩
from score;
### 查询所有选修“计算机导论”课程的“男”同学的成绩表 要求Cname 计算机导论 Ssex 男
select Degree 成绩
from score
where Sno in (
select Sno
from student
where Ssex = '男'
)
and Cno = (
select Cno
from course
where Cname = '计算机导论'
);
#### 我™原来子查询要脱离连接查询才叫子查询啊。
#### 不能声明超出查询字段名之外的字段才行
select Sno, Degree
from Score
where Sno in (
select Sno
from student
where Ssex = '男'
)
and Cno in (
select Cno
from Course
where Cname = '计算机导论'
);
# 3、查询和“李军”同性别的所有同学的Sname.
select Sname 学生姓名
from student
where Ssex = (
select Ssex
from student
where Sname = '李军'
)
and Sname not in ('李军');
## 这就是你要的有李军但是不包括李军。
select Sname
from student
where Ssex = (
select Ssex
from student
where Sname = '李军'
)
and Sname not in ('李军');
# 4、查询和“李军”同性别并同班的同学Sname.
select Sname 学生姓名
from student
where (Ssex, Class) = (
select Ssex, Class
from student
where Sname = '李军'
)
and Sname not in ('李军');
## 有李军但是不包括李军?
select Sname
from student
where Ssex = (
select Ssex
from student
where Sname = '李军'
)
and Sname not in ('李军')
and Class = (
select Class
from student
where Sname = '李军'
);
# 5、查询最高分同学的Sno、Cno和Degree列。√
select Sno 学号, Cno 课程号, Degree 成绩
from score
where Degree = (
select max(Degree)
from score
);
## 和第1、差不多
select Sno, Cno, Degree
from Score
where degree = (
select MAX(Degree)
from Score
);
# 6、 查询所有未讲课的教师的Tname和Depart. ***
select Tname 教师姓名, Depart 教师单位
from teacher
where Tno not in (
select Tno
from course
);
## 百度 写的好难懂哦
select Tname, Depart
from Teacher
where Tname not in (
select distinct Tname
from Teacher,
Course,
Score
where Teacher.Tno = Course.Tno
and Course.Cno = Score.Cno
);
# 7、查询所有任课教师的Tname和Depart. ***
select Tname 教师姓名, Depart 教师单位
from teacher
where Tno in (
select Tno
from course
);
## 和第6、是相反的
select Tname, Depart
from Teacher
where Tname in (
select distinct Tname
from Teacher,
Course,
Score
where Teacher.Tno = Course.Tno
and Course.Cno = Score.Cno
);
# 8、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select Sno 学号, Cno 课程号, Degree 成绩
from score
where Cno = '3-105'
and Degree > all (
select Degree
from score
where Cno = '3-245'
);
## all是关键所在吧
# 10、查询出“计算机系”教师所教课程的成绩表。
select Degree
from score
where Cno in (
select Cno
from course
where Tno in (
select Tno
from teacher
where Depart = '计算机系'
)
);
## in很关键
select sno, Cno, Degree
from Score
where Cno in (
select Cno
from Course
where Tno in (
select tno
from Teacher
where Depart = '计算机系'
)
);
# 11、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。***
select Tname 教师姓名, Prof 职称
from teacher
where Depart = '计算机系'
and Prof not in (
select Prof
from teacher
where Depart = '电子工程系'
);
## 这波儿 not in 很关键!
select Tname, Prof
from Teacher a
where Prof not in (
select Prof
from Teacher b
where a.Depart != b.Depart
);
# 12、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select Sno 学号, Cno 课程号, Degree 成绩
from score
where Cno = '3-105'
and Degree >= any (
select Degree
from score
where Cno = '3-245'
)
order by Degree desc;
## 至少高于 就是只要高于 3-245 的一项就行
select Cno, Sno, Degree
from Score a
where (
select Degree
from Score b
where Cno = '3-105'
and b.Sno = a.Sno ### 这个是什么意思呢?
) >= (
select Degree
from Score c
where Cno = '3-245'
and c.Sno = a.Sno
)
order by Degree desc;
# 13、查询选修某课程的同学人数多于5人的教师姓名。
select Tname 教师姓名
from teacher
where Tno in (
select Tno
from course
where Cno in (
select Cno
from score
group by Cno
having count(Sno) > 5
)
);
## 这一题贼难,百度解决的……
select Tname
from Teacher
where Tno in (
select Tno
from Course
where Cno in (
select Cno
from Score
group by Cno
having COUNT(*) > 5
)
);
# 14、查询存在有85分以上成绩的课程Cno.
select Cno 课程编号
from score
where Degree in (
select Degree
from score
where Degree > 85
);
## 我这写的相当问题啊
select distinct cno
from Score
where Degree > 85;
# 15、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select *
from score
where Degree > (
select Degree
from score
where Sno = '109'
and Cno = '3-105'
);
select *
from student,
Score
where student.Sno = Score.Sno
and Score.Degree > (
select Degree
from Score
where Cno = '3-105'
and Sno = '109'
);
# 16、查询“张旭“教师任课的学生成绩。
select Degree 成绩
from score
where Cno in (
select Cno
from course
where Tno in (
select Tno
from teacher
where Tname = '张旭'
)
);
select Degree
from Score,
Teacher,
Course
where Teacher.Tname = '张旭'
and Teacher.Tno = Course.Tno
and Course.Cno = Score.Cno;
# 17、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sage列。
select Sno 学号, Sname 学生姓名, Sage 学生年龄
from student
where Sage = (
select Sage
from student
where Sno = '108'
);
五、matome
- 子查询这块儿真的相当难了,吐了🤮
- 2020年11月17日 周二 下午下雨了 我不喜欢下雨,昏昏沉沉的想睡觉,啊我没充上饭卡
- 我容易被学了啥就用啥这个思维限制住,但是,学以致用不是吗【doge】
- 自连接那里也有点晕,我承认我心急了,但是时间都过去86天了,我……我还得更加努力才行啊。
- 是的,时间总会带来些什么,比如……答案,没错,时间会给我答案,我的滑板鞋,时尚时尚很时尚……【doge】
- 我努力地说服自己,别重复地做一件事,应该去更远的地方看看,看看自己做的事是不是有意义。比如,叔~装个系统吧,装的好了,下次再来哈……但是有时候远方也是不如意的,还不如回家娶媳妇🤭
- 听某Java说,编程的海洋很广阔,穷尽一生也是学不完的,对啦,知识都是这样的么。

浙公网安备 33010602011771号