【MySQL】子查询

本文详细介绍了SQL中的子查询概念,包括单行、多行和多列子查询的用法,并通过实例展示了如何在查询中使用子查询。此外,还探讨了ANY和ALL关键字在比较子查询结果中的应用。同时,文章通过实际的数据库操作,演示了如何利用子查询作为临时表进行复杂的数据分析,如查找部门平均工资以上的员工、最高工资员工以及部门员工数量等信息。
摘要由CSDN通过智能技术生成

子查询:嵌入在其他sql语句中的select语句,也叫做嵌套语句。

单行子查询(只返回一行数据的子查询语句);

多行子查询(返回多行数据的子查询语句),使用关键字in;

多列子查询(返回多列数据的子查询语句)。

子查询还可以当成临时表使用。

#子查询
#创建员工表emps,部门表dept,工资级别表salgrade
-- create table emps (empno int,ename varchar(256),job varchar(256),mgr int,birthday datetime,sal double,comm double,deptno int);
-- create table dept(deptno int,deptname varchar(256),loc varchar(256));
-- create table salgrade(grade int,lowsal double,highsal double);
-- insert into emps values(1234,'abby','cleck',3456,'1990-12-12',8000,null,10),
--                        (2345,'bitty','saleman',1234,'1987-12-12',6000,500,20),
--                        (3456,'cindy','manager',null,'1980-12-12',120000,1200,30),
--                        (4567,'doller','cleck',1234,'2000-12-12',7000,800,10),
--                        (5678,'eggie','saleman',4567,'1994-12-12',9000,1000,20);
-- select * from emps;
-- insert into dept values(10,'accouting','shanghai'),						 
--                         (20,'sales','beijing'),
-- 												(30,'management','guangzhou');
-- select * from dept;
-- insert into salgrade values(1,5000,6500),(2,6800,7500),(3,8000,15000);		
-- select * from salgrade;	

#单行子查询
#查询与abby同一部门的职工
select * from emps where deptno = (select deptno from emps where ename = 'abby');

#多行子查询
#查询和部门10的工作相同的职工名字,岗位,工资,部门号,但不包含10
#步骤:
# ①查询出跟部门10工作相同的工作有哪些  select distinct job from emps where deptno = 10
# ②将上边语句当子查询使用 
select ename,job,sal,deptno from emps where job in (select distinct job from emps where deptno = 10 ) and deptno <> 10

#any,all
#显示工资比部门30所有职工高的职工名字,工资和部门号
select ename,sal,deptno from emps where sal > all (select sal from emps where deptno = 30)
#显示工资比部门30其中任一职工高的职工名字,工资和部门号
select ename,sal,deptno from emps where sal > any (select sal from emps where deptno = 30)

#多列子查询
#查询与abby部门岗位完全相同的职工(不包括abby)
select * from emps where (job,deptno) = (select job,deptno from emps where ename = 'abby') and ename !='abby'

#子查询可当为临时表使用
#查询每个部门工资高于本部门平均工资的职工
#步骤:
#① 查询每个部门部门号和平均工资
#select deptno,avg(sal) from emps group by deptno
#②以①查询结果为子查询,跟emps表多表查询
select * from emps,(select deptno,avg(sal) as avg_sal from emps group by deptno) as other where emps.deptno = other.deptno and emps.sal > other.avg_sal 

#查询每个部门工资最高的职工
#每个部门部门号和最高工资 select deptno,max(sal) from emps group by deptno
select * from emps,(select deptno,max(sal) as max_sal from emps group by deptno)temp where temp.deptno = emps.deptno and emps.sal = temp.max_sal

#查询每个部门部门名称,部门编号,地址和职工数量
#查询出每个部门编号和职工数量 select count(*),deptno from emps group by deptno
select deptname,dept.deptno,loc,temp.total from dept,(select count(*) as total,deptno from emps group by deptno)temp where dept.deptno = temp.deptno 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值