高级SQL语句查询(含答案和截图)

  • 格式:docx
  • 大小:65.07 KB
  • 文档页数:7

C顾客cidcnamecity discntc001 李广天津10.00c002 王开基北京12.00c003 安利德北京8.00c004 曹士雄天津8.00c006 曹士雄广州0.00P商品pidpname city quantity pricep01 梳子天津111400 0.50p02 刷子成都203000 0.50p03 刀片西安150600 1.00p04 钢笔西安125300 1.00p05 铅笔天津221400 1.00p06 文件夹天津123100 2.00p07 盒子成都100500 1.00A代理aidanamecity percenta01 史可然北京 6a02 韩利利上海 6a03 杜不朗成都7a04 甘瑞北京 6a05 敖斯群武汉 5a06 史可然天津 5O订单ordnomonthcidaid pidqtydollars1011 01 c001 a01 p01 1000 450.00 1012 01 c001 a01 p01 1000 450.00 1019 02 c001 a02 p02 400 180.00 1017 02 c001 a06 p03 600 540.00 1018 02 c001 a03 p04 600 540.00 1023 03 c001 a04 p05 500 450.00 1022 03 c001 a05 p06 400 720.00 1025 04 c001 a05 p07 800 720.001013 01 c002 a03 p03 1000 880.001026 05 c002 a05 p03 800 704.001 查询所有定购了至少一个价值为0.50的商品的顾客的名字。

amefrom clientwherecid in (select cid from order1 where pid in(select pid from product where price='0.5'))2 找出全部没有在代理商a03处订购商品的顾客cid值。

selectclient.cidfrom clientwherecid not in (select cid from order1 where aid in(select aid from agend where aid='a03'))3 找出只在代理商a03处订购商品的顾客。

selectclient.cidfrom clientwherecid not in (select cid from order1 where aid in(select aid from agend where aid!='a03') )AND cid in (select cid from order1 where aid in(select aid from agend where aid='a03'))4 找出没有被任何一个在北京的顾客通过在上海的代理商订购的所有商品。

select *from productwherepid not in (select pid from client,agend,order1where client.cid=order1.cid and agend.aid = order1.aid and client.city='北京' and agend.city = '上海')5 找出订购了所有价格为0.50的商品的顾客名字。

selectcnamefrom clientwherecid in(select cid from order1 ,product where order1.pid=product.pid AND product.price='0.50' )6 找出订购所有被任何人订购过一次的商品的顾客cid。

除法selectcidfrom clientwhere not exists(select pid from order1 where pid not in(select pid from order1 where order1.cid=client.cid))7 找出所有接到至少顾客c004订购的商品集合的订单的代理商的aid。

select aidfrom order1wherecid='c004'8 找出订购了p01和p07这两种商品的顾客的cid。

错误:select cidfrom order1wherepid='p01' AND pid='p07'正确:select distinct cidfrom order1wherepid='p01' AND exists(select pid from order1 where pid='p07')9 找出从至少一个接受订购商品p03订单的代理商处订购过商品的顾客cid。

select distinct cidfrom order1where aid in (select aid from order1 where pid='p03')10 找出所有具有和在北京和天津的顾客相同的折扣率的顾客的cid。

selectcidfrom clientwhere discnt in (select discnt from client where city='北京' )AND discnt in (select discnt from client where city='天津' )11 列出通过以下代理商订购的商品的pid:代理商从以下的顾客处接受订单,而这些顾客从一个接受过顾客c001订单的代理商处订购了至少一个商品。

select distinctpidfrom order1where aid in(select aid from order1 where cid in(select cid from order1 where aid in (select aid from order1 where cid='c001')))12 在orders表上生成每笔业务的利润profit(收入减去60%的成本、顾客的折扣以及代理商的酬金)。

select ordno,x.cid,x.aid,x.pid,.40*(x.qty*p.price)-.01*(c.discnt+a.percent)*(x.qty*p.price) as profit from order1 as x,client as c,agend as a,product as pwherec.cid=x.cid and a.aid=x.aid and p.pid=x.pid13 求通过住在北京或上海的代理商订货的顾客的姓名和折扣。

selectcname,discntfrom clientwhere cid in(select cid from order1 where aid in(select aid from agend where city='北京' or city='上海'))14 求出满足以下条件的顾客cid:该顾客的discnt小于任一住在北京的顾客的discnt.SELECT cidfrom clientwhere discnt<all(select discnt from client where city='北京' )15 求出既订购了产品p01有订购了产品p07的顾客cid.SELECT distinctcidfrom order1wherepid='p01' AND EXISTS(select pid from order1 where pid='p07')16 找出没有通过代理商a03订货的顾客cid.selectclient.cidfrom clientwherecid not in (select cid from order1 where aid in(select aid from agend where aid='a03'))17 检索至少订购了一件价格低于0.5的商品的顾客姓名。

SELECT cnamefrom client,order1whereclient.cid=order1.cid AND pid in (select pid from product where price< '0.50')18 打印每个代理商为顾客c002和c003订购产品及产品总数量.selectaid,pid,sum(qty)as amountfrom order1wherecid='c002' or cid='c003'group by aid,pid19 求被至少两个顾客订购的产品pid.1)selectpidfrom order1group by pidhaving count(distinct cid)>=22)selectpidfrom productwhere 2<=all(select count(distinct cid) from order1 where order1.pid=product.pid)。