SQL嵌套子查询

来源:互联网 发布:软件需求说明书 简洁 编辑:程序博客网 时间:2024/04/30 13:31
--显示工资大于等于wh1仓库中所有职工工资的职位信息
select * from 职工 where 工资>= all(select 工资 from 职工 where 仓库号='wh1');select * from 职工 where 工资>= (select MAX(工资) from 职工 where 仓库号='wh1');


--select * from 仓库 where 面积=(select 面积 from 仓库 where 仓库号='wh1')--select * from 仓库 where 面积=(select 面积 from 仓库 where 仓库号='wh1') and 仓库号!='wh1'--select * from 仓库 where 面积=(select MAX(面积) from 仓库)--select * from 仓库 where 面积!=(select MIN(面积) from 仓库);

--select * from 职工 where 工资!>(select AVG(工资) from 职工 where 仓库号='wh1')--  and 工资 !> (select AVG(工资) from 职工 where 仓库号='wh2');--select * from 职工 where 工资!>(select AVG(工资) from 职工 where 仓库号='wh1' or 仓库号='wh2');--select * from 仓库 where 面积>(select AVG(面积) from 仓库 where 城市='北京') or--  面积<(select MIN(面积) from 仓库 where 城市='济南')

--显示工资不大于北京地区的平均工资的职工信息--select * from 职工 where 工资!>(select AVG(工资) from 职工 where 仓库号 in --   (select 仓库号 from 仓库 where 城市='北京'))--select * from 仓库 where 面积 in ((select MAX(面积) from 仓库), (select MIN(面积) from 仓库));


--显示仓库面积最大或者最小的职工信息select * from 职工 where 仓库号 in (    select 仓库号 from 仓库 where 面积 in    (      (select MAX(面积) from 仓库),      (select MIN(面积) from 仓库)    ) )


--显示仓库面积最大或者最小的职工的订购单信息select * from 订购单 where 职工号 in(select 职工号 from 职工 where 仓库号 in (    select 仓库号 from 仓库 where 面积 in    (      (select MAX(面积) from 仓库),      (select MIN(面积) from 仓库)    ) ) )

--显示工资不是最高也不是最低的职工所在的仓库信息select * from 仓库 where 仓库号 not in(  select 仓库号 from 职工 where 工资=(select MAX(工资) from 职工)  or 工资=(select MIN(工资) from 职工)  )

--显示没有订单的职工信息select * from 职工 where 职工号 not in(select 职工号 from 订购单);

exists运算符在嵌套子查询中的运用
--查询有职工的仓库信息select * from 仓库 where exists(select * from 职工 where 仓库号=仓库.仓库号);

--查询有职工并且工资大于2000的仓库信息select * from 仓库 where exists(select * from 职工 where 仓库号=仓库.仓库号 and 工资>2000);

--查询有职工并且工资大于2000的仓库信息select * from 仓库 where exists(select * from 职工 where 仓库号=仓库.仓库号) and 城市 is not null;

any运算符在嵌套子查询中的运用

any运算符常常用在比较运算中,只要嵌套子查询中有一行能使结果为真,则结果为真。

--查询工资大于wh1仓库中任何一名职工工资的职工信息select * from 职工 where 工资> any(select 工资 from 职工 where 仓库号='wh1')select * from 职工 where 工资> (select MIN(工资) from 职工 where 仓库号='wh1')

--查询工资小于wh1仓库中任何一名职工工资的职工信息select * from 职工 where 工资< any(select 工资 from 职工 where 仓库号='wh1')select * from 职工 where 工资< (select MAX(工资) from 职工 where 仓库号='wh1')

--查询姓名中含有"平", 并且工资大于姓名中含有"王"字的任一名职工工资的职工信息select * from 职工 where 姓名 like '%平%' and 工资> any(select 工资 from 职工 where 姓名 like '%王%')

all运算符在嵌套子查询中的运用

all运算符要求嵌套子查询中所有行都使结果为真,结果才为真

--显示工资大于等于wh2仓库中所有职工工资的职位信息select * from 职工 where 工资> all(select 工资 from 职工 where 仓库号='wh2');


--显示工资大于等于wh1仓库中所有职工工资的职位信息select * from 职工 where 工资>= all(select 工资 from 职工 where 仓库号='wh1');select * from 职工 where 工资>= (select MAX(工资) from 职工 where 仓库号='wh1');

--显示工资小于等于wh1仓库中所有职工工资的职位信息select * from 职工 where 工资<= all(select 工资 from 职工 where 仓库号='wh1');select * from 职工 where 工资<= (select MIN(工资) from 职工 where 仓库号='wh1');
--按平均工资从高到低显示不同仓库的仓库号、平均工资、职工人数、最大工资信息select * from (select 仓库号, AVG(工资) as 平均工资, (SUM(工资)/AVG(工资)) as 职工人数,MAX(工资) as 最大工资 from 职工 group by 仓库号)mytable order by 平均工资 


select * from 订购单;select *, (select AVG(金额) from 订购单) as 平均销售金额,          (金额-(select AVG(金额) from 订购单)) as 与平均销售金额之差 from 订购单

select 姓名,工资, 不同仓库的平均工资 from (select *, 不同仓库的平均工资= case       when 仓库号='wh1' then (select AVG(工资) from 职工 where 仓库号='wh1')      when 仓库号='wh2' then (select AVG(工资) from 职工 where 仓库号='wh2')      when 仓库号='wh3' then (select AVG(工资) from 职工 where 仓库号='wh3')      when 仓库号='wh4' then (select AVG(工资) from 职工 where 仓库号='wh4') end from 职工) mytable 


--显示工资大于不同仓库平均工资的职工信息select * from 职工 where 工资> all(select AVG(工资) from 职工 group by 仓库号)

--显示工资大于任意一个仓库平均工资的职工信息select * from 职工 where 工资> any(select AVG(工资) from 职工 group by 仓库号) 

--显示不同职工经手订单金额最大的订单信息select a.* from 订购单 a where 金额=(select MAX(金额) from 订购单 where 职工号=a.职工号) 



--显示不同仓库的最大工资对应的职工信息select a.* from 职工 a  where 工资=(select MAX(工资) from 职工 where 仓库号=a.仓库号);





原创粉丝点击