栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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


哈喽!大家好,我是【IT邦德】,江湖人称jeames007,10年DBA工作经验
一位上进心十足的【大数据领域博主】!
中国DBA联盟(ACDU)成员,目前从事DBA及程序编程,B站及腾讯课堂讲师,直播量破10W
擅长主流数据Oracle、MySQL、PG 运维开发,备份恢复,安装迁移,性能优化、故障应急处理等。
✨ 如果有对【数据库】感兴趣的【小可爱】,欢迎关注【IT邦德】
❤️❤️❤️感谢各位大可爱小可爱!❤️❤️❤️

SQL对于现在的互联网公司产研岗位几乎是一个必备技能,但仅会SQL的话,应该是什么都做不了。
1.如果你是数据分析师,你需要熟练地把自己脑子里的数据和指标需求翻译成SQL逻辑去查询数据,进而完成自己的数据分析报告等,你的产出是分析报告,而不是SQL代码;
2.如果你是数仓工程师(偏应用层),你需要根据业务逻辑去设计模型,编写调度任务去产出数据,以供业务人员使用,你的产出是数据模型和表;
3.如果你是算法工程师,你可能需要用SQL来实现用户标签、特征工程等工作,但是这些是为你的模型训练评估做基础准备工作,你的产出是可以提升某些指标的算法模型。

所以,SQL每个人都要用,但是用来衡量产出的并不是SQL本身,你需要用这个工具,去创造其它的价值。
IT邦德老师带你成为SQL高手,那我们开始吧~

文章目录

⛳️1.子查询

 实例1实例2实例3实例4实例5实例6实例7实例8 ⛳️2.联合查询

实例1实例2实例3

本文案例导入以下sql文本即可,通过百度网盘下载
mysql> source h:db_school.sql
网盘链接:https://pan.baidu.com/s/1rvJhB6it8rvOMeqMjRAGlg?pwd=0hx1

⛳️1.子查询  实例1

查询选修了课程的学生姓名
select a.studentName from tb_student a where a.studentNo in
(select b.studentNo from tb_score b);

实例2

查询没有选修过课程的学生姓名。
select a.studentName from tb_student a where a.studentNo not in
(select b.studentNo from tb_score b);

实例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班’);

实例4

查询与‘李明’同班的学生学号、姓名和班级编号。
select * from tb_student a
where a.classNo in
(select b.classNo from tb_student b where b.studentName=‘李明’)
and a.studentName != ‘李明’;

实例5

查询男生中比任意一个女生出生年份都晚的学生姓名和出生年份。
select * from tb_student a where a.birthday > any
(select birthday from tb_student b where b.sex =‘女’)
and a.sex =‘男’;

实例6

查询选修了课程号为‘31002’的学生姓名。
select a.studentName from tb_student a
where a.studentNo in
(select b.studentNo from tb_score b where b.courseNo=‘31002’);

实例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’);

实例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)
);

⛳️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=‘计算机基础’;

实例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=‘计算机基础’;

实例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<>‘管理学’;


大家点赞、收藏、关注、评论啦 微信公众号

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号