【如何成为SQL高手】第八关:子查询及联合查询

简介: 【如何成为SQL高手】第八关:子查询及联合查询

1.子查询



😆 实例1


查询选修了课程的学生姓名

select a.studentName from tb_student a where a.studentNo in

(select b.studentNo from tb_score b);

7706f55b41154c46a3695542f801dbb0.png


😆实例2


查询没有选修过课程的学生姓名。

select a.studentName from tb_student a where a.studentNo not in

(select b.studentNo from tb_score b);


0f4a501b99ab462781d0adbe1fbed9a7.png


😆实例3


查询班级‘计算机14-1班’所有学生的学号和姓名。

select a.studentNo,a.studentName from tb_student a where a.classNo in

(select b.classNo from tb_class b where b.className =‘计算机14-1班’);

6ae4b1354bd44fa0bc1220eaab32b003.png



😆实例4


查询与‘李明’同班的学生学号、姓名和班级编号。

select * from tb_student a

where a.classNo in

(select b.classNo from tb_student b where b.studentName=‘李明’)

and a.studentName != ‘李明’;


24887102b7334dd19ab543948211f186.png

😆实例5


查询男生中比任意一个女生出生年份都晚的学生姓名和出生年份。

select * from tb_student a where a.birthday > any

(select birthday from tb_student b where b.sex =‘女’)

and a.sex =‘男’;

b832c81ecffa40a886d8592ab2ec8e03.png


😆实例6


查询选修了课程号为‘31002’的学生姓名。

select a.studentName from tb_student a

where a.studentNo in

(select b.studentNo from tb_score b where b.courseNo=‘31002’);


86ba21af672d479fbb77ccb2c03fdb9a.png


😆实例7


查询没有选修课程号为‘31002’的学生姓名。

select a.studentName from tb_student a

where a.studentNo not in

(select b.studentNo from tb_score b where b.courseNo=‘31002’);

c01a180f6dbd47429d0663c170e64717.png


😆实例8


查询选修了全部课程的学生姓名

select a.studentName from tb_student a

where a.studentNo in (

select studentNo from tb_score group by studentNo

having count() = (select count() from tb_course)

);

dcd1048073644009ad1cb8c03334d644.png



⛳️2.联合查询



😆实例1


使用UNION查询选修了‘管理学’或者‘计算机基础’的学生学号

select b.studentNo

from tb_score a,tb_student b,tb_course c

where a.studentNo = b.studentNo

and a.courseNo = c.courseNo

and c.courseName=‘管理学’

union

select b.studentNo

from tb_score a,tb_student b,tb_course c

where a.studentNo = b.studentNo

and a.courseNo = c.courseNo

and c.courseName=‘计算机基础’;


d9bee26749a54b308e7ae44162854000.png

😆实例2


使用UNION ALL查询选修了‘管理学’或者‘计算机基础’的学生学号。

select b.studentNo

from tb_score a,tb_student b,tb_course c

where a.studentNo = b.studentNo

and a.courseNo = c.courseNo

and c.courseName=‘管理学’

union all

select b.studentNo

from tb_score a,tb_student b,tb_course c

where a.studentNo = b.studentNo

and a.courseNo = c.courseNo

and c.courseName=‘计算机基础’;

e54c8e006c474e33b9f14ac3a7132fcd.png


😆实例3


查询选修了‘计算机基础’,但没有选修‘管理学’的学生学号。

select b.studentNo

from tb_score a,tb_student b,tb_course c

where a.studentNo = b.studentNo

and a.courseNo = c.courseNo

and c.courseName=‘计算机基础’

union

select b.studentNo

from tb_score a,tb_student b,tb_course c

where a.studentNo = b.studentNo

and a.courseNo = c.courseNo

and c.courseName<>‘管理学’;

43fa6fdb58ea4bd8bc3b63e14763ffce.png

目录
相关文章
|
4月前
|
SQL 数据库
SQL Server 连接查询和子查询
SQL Server 连接查询和子查询
91 0
|
6月前
|
SQL 数据库
达梦(DM) SQL查询及联合查询
继续讲解达梦(DM)数据库SQL查询操作
|
7月前
|
SQL
sql语言之子查询语句
sql语言之子查询语句
|
3月前
|
SQL Oracle 关系型数据库
七、SQL子查询
七、SQL子查询
32 0
|
9月前
|
SQL
SQL进阶之疯狂的子查询
今天我们不谈子查询的执行效率,只谈子查询功能。对于SQL语句来说,编程开发人员应该都不会陌生,其实各种复杂的逻辑最终落地也就是那么几个基本的招式,增删改查,然而我们经常听到武功招式中又各种连招,那就是了,子查询就是这么个意思。很多SQL简单查询完成不了的查询,子查询都可以很疯狂地完成。当然有的需要一个子查询就够了,有的甚至可能需要嵌套多个子查询来完成。
54 0
SQL进阶之疯狂的子查询
|
9月前
|
SQL 数据库
SQL中字段跟子查询结果判等,子查询结果为null的处理办法
前言:今天写功能的时候遇到一个问题,SQL中字段跟子查询结果判等,子查询结果为null,查询不到数据
|
5月前
|
SQL
SQL联合查询
SQL联合查询
|
9月前
|
SQL Oracle 关系型数据库
update时 单行子查询返回多个行 SQL 错误 [1427] 处理方案
我遇到此错误是在多表关联update的
413 0
|
9月前
|
SQL 关系型数据库 数据库
10个高级SQL写法,包括窗口函数、联合查询、交叉查询、递归查询
10个高级SQL写法,包括窗口函数、联合查询、交叉查询、递归查询
167 1
|
SQL Oracle 关系型数据库
【SQL系列】标量子查询
嵌套在 SELECT 子句中的 SELECT 子句被称为标量子查询,它们只能返回一个值。
250 0