SQL语句练习——查询

数据表如下:

1.用SQL完成以下检索:

检索在北京的供应商的名称。

select 供应商名 from 供应商 where 地址 ='北京'

检索发给供应商S6的订购单号。

select 订购单号 from 订购单 where 供应商号='S6'

检索出职工E6发给供应商S6的订购单信息。

select * from 订购单 where 职工号='E6'and 供应商号='S6'

检索出向供应商S3发过订购单的职工的职工号和仓库号。

select 职工号,仓库号 from 职工 where 职工号 in (select 职工号 from 订购单 where 供应商号='S3' and 职工.职工号=订购单.职工号)

检索出目前与S3供应商没有联系的职工信息。

select* from 职工 where 职工号 not in (select 职工号 from 订购单 where 供应商号='S3'and 订购单.职工号=职工.职工号)

检索出目前没有任何订购单的供应商信息。

select * from 供应商 where 供应商号 not in (select 供应商号 from 订购单 where 订购单.供应商号=供应商.供应商号)

检索出和职工E1、E3都有联系的北京的供应商信息。

select * from 供应商 where 地址='北京' and 供应商号 in (select 供应商号 from 订购单 where 订购单.供应商号=供应商.供应商号 and 职工号='E1')
and 供应商号 in(select 供应商号 from 订购单 where 订购单.供应商号=供应商.供应商号 and 职工号='E1')

检索出目前和华通电子公司有业务联系的每个职工的工资。

select 工资 from 职工 where 职工号 in (select 职工号 from 订购单 where 供应商号 in (select 供应商号 from 供应商 where 供应商.供应商号=订购单.供应商号 and 供应商名='华通电子公司')and 订购单.职工号=职工.职工号)

检索出与工资在1220元以下的职工没有联系的供应商的名称。

select 供应商名 from 供应商 where 供应商号 in 
(select 供应商号 from 订购单 where 订购单.供应商号=供应商.供应商号 and 职工号 in 
(select 职工号 from 职工 where 工资<1220 and 职工.职工号=订购单.职工号))

检索出向S4供应商发出订购单的仓库所在的城市。

select 城市 from 仓库 where 仓库号 in
(select 仓库号 from 职工 where 职工.仓库号=仓库.仓库号 and 职工号 in
(select 职工号 from 订购单 where 订购单.职工号=职工.职工号 and 供应商号='S4' ))

检索出在上海工作并且向S6供应商发出了订购单的职工号。

select 职工号 from 职工 where 仓库号 in 
(select 仓库号 from 仓库 where 城市='上海' and 仓库.仓库号=职工.仓库号)
and 职工号 in
(select 职工号 from 订购单 where 订购单.职工号= 职工.职工号 and 供应商号='S6')

检索出在广州工作并且只向S4供应商发出了订购单的职工号。

select 职工号 from 职工 where 仓库号 in 
(select 仓库号 from 仓库 where 城市='广州' and 仓库.仓库号=职工.仓库号)
and 职工号 in
(select 职工号 from 订购单 where 订购单.职工号= 职工.职工号 and 供应商号='S4')
and 职工号 not in
(select 职工号 from 订购单 where 订购单.职工号= 职工.职工号 and 供应商号<>'S4')

检索出由工资多于1230元的职工向北京的供应商发出的订购单号。

select 订购单号 from 订购单 where 供应商号 in 
(select 供应商号 from 供应商 where 地址='北京' and 供应商.供应商号=订购单.供应商号) 
and 职工号 in 
(select 职工号 from 职工 where 职工.职工号=订购单.职工号 and 工资 >1230)

检索出仓库的个数。

select COUNT(*) as 仓库个数 from 仓库

检索出有最大面积的仓库信息。

select * from 仓库 where 面积=(select max(面积) from 仓库 )

检索出所有仓库的平均面积。

select AVG(面积) as 平均面积 from 仓库

检索出向S4供应商发出订购单的那些仓库的平均面积。

select AVG(面积) as 平均面积 from 仓库 where 仓库号 in 
(select 仓库号 from 职工 where 职工.仓库号=仓库.仓库号 and 职工号 in
(select 职工号 from 订购单 where 订购单.职工号=职工.职工号 and 供应商号='S4') )

检索出每个城市的供应商个数。

select 地址,COUNT(供应商号) as 数量 from 供应商 group by 地址

检索出每个仓库中工资多于1220元的职工个数。

select 仓库号,COUNT(职工号) as 数量 from 职工 where 工资 >1220 group by 仓库号

检索出和面积最小的仓库有联系的供应商的个数。

select COUNT(供应商号) as 数量 from 供应商 where 供应商号 in 
(select 供应商号 from 订购单 where 订购单.供应商号=供应商.供应商号 and 职工号 in 
(select 职工号 from 职工 where 职工.职工号=订购单.职工号 and 仓库号 in 
(select 仓库号 from 仓库 where 面积 in 
(select MIN(面积) from 仓库))) )

检索出工资低于本仓库平均工资的职工信息。

select * from 职工 A where A.工资  <(select AVG(工资) from 职工 B  where A.仓库号=B.仓库号 group by 仓库号)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值