【如何成为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

目录
相关文章
|
SQL 数据库
SQL Server 连接查询和子查询
SQL Server 连接查询和子查询
388 0
|
SQL 数据库
达梦(DM) SQL查询及联合查询
继续讲解达梦(DM)数据库SQL查询操作
sql语言之子查询语句
sql语言之子查询语句
|
8月前
|
SQL 关系型数据库 PostgreSQL
CTE vs 子查询:深入拆解PostgreSQL复杂SQL的隐藏性能差异
本文深入探讨了PostgreSQL中CTE(公共表表达式)与子查询的选择对SQL性能的影响。通过分析两者底层机制,揭示CTE的物化特性及子查询的优化融合优势,并结合多场景案例对比执行效率。最终给出决策指南,帮助开发者根据数据量、引用次数和复杂度选择最优方案,同时提供高级优化技巧和版本演进建议,助力SQL性能调优。
871 1
|
SQL 安全 前端开发
Web学习_SQL注入_联合查询注入
联合查询注入是一种强大的SQL注入攻击方式,攻击者可以通过 `UNION`语句合并多个查询的结果,从而获取敏感信息。防御SQL注入需要多层次的措施,包括使用预处理语句和参数化查询、输入验证和过滤、最小权限原则、隐藏错误信息以及使用Web应用防火墙。通过这些措施,可以有效地提高Web应用程序的安全性,防止SQL注入攻击。
594 2
|
SQL 关系型数据库 PostgreSQL
遇到SQL 子查询性能很差?其实可以这样优化
遇到SQL 子查询性能很差?其实可以这样优化
705 2
SQL进阶之疯狂的子查询
今天我们不谈子查询的执行效率,只谈子查询功能。对于SQL语句来说,编程开发人员应该都不会陌生,其实各种复杂的逻辑最终落地也就是那么几个基本的招式,增删改查,然而我们经常听到武功招式中又各种连招,那就是了,子查询就是这么个意思。很多SQL简单查询完成不了的查询,子查询都可以很疯狂地完成。当然有的需要一个子查询就够了,有的甚至可能需要嵌套多个子查询来完成。
439 0
SQL进阶之疯狂的子查询
|
SQL 数据挖掘 数据库
SQL 子查询深度剖析来袭!嵌套查询竟有如此无限可能,带你轻松玩转复杂数据检索与操作!
【8月更文挑战第31天】在 SQL 中,子查询是一种强大的工具,允许在一个查询内嵌套另一个查询,从而实现复杂的数据检索和操作。子查询分为标量子查询、列子查询和行子查询,可用于 SELECT、FROM、WHERE 和 HAVING 子句中。例如,查找年龄大于平均年龄的学生或每个课程中成绩最高的学生。子查询具有灵活性、可重用性和潜在的性能优化优势,但需注意性能问题、可读性和数据库支持。合理使用子查询能够显著提升查询效率和代码维护性。
418 1
|
SQL
什么是SQL中的子查询?
【8月更文挑战第2天】什么是SQL中的子查询?
263 1
|
Java 网络架构 数据格式
Struts 2 携手 RESTful:颠覆传统,重塑Web服务新纪元的史诗级组合!
【8月更文挑战第31天】《Struts 2 与 RESTful 设计:构建现代 Web 服务》介绍如何结合 Struts 2 框架与 RESTful 设计理念,构建高效、可扩展的 Web 服务。Struts 2 的 REST 插件提供简洁的 API 和约定,使开发者能快速创建符合 REST 规范的服务接口。通过在 `struts.xml` 中配置 `&lt;rest&gt;` 命名空间并使用注解如 `@Action`、`@GET` 等,可轻松定义服务路径及 HTTP 方法。
270 0