SQL重要的常用查询语句(2)

2020-04-17 03:12

select distinct khm,spm,lb from khb a join xsb b on a.khh = b.khh join spb c on c.sph = b.sph

where a.khh in (

select khh from xsb join spb on xsb.sph = spb.sph where lb = '电视') and a.khh in (

select khh from xsb join spb on xsb.sph = spb.sph where lb = '冰箱') 题型:至少型。

步骤:1、要显示的字段,

2、类别不能同时为电视和冰箱,所以必须找一个字段来连接,商品号不行,针对题意商品号可以。中间用and运算。

--6. 在客户表中插入一新记录,客户号为“K100”,客户名为“新客户”,积分用默认值。 insert into khb(khh,khm) values('K100','新客户') 题型:常规

--7. 将销售总数量超过1000的商品的进价降低10%。 update spb set jj = jj * 0.9 where sph in (

select sph from xsb group by sph having sum(xssl) > 1000)

题型:常规:往往会有一个子查询

--1、查询2010年以前单笔销售数量最少的销售记录 --子查询 select * from xsqkb

where year(xsrq)<2010 and xssl=( )

--使用top

select top 1 with ties * from xsqkb

where year(xsrq)<2010 order by xssl

题型:排序型

--2、查询由多个产地生产的相同类别的产品,列出产品名称,类别及产地 --子查询

select cpmc,lb,cd from cpb

select min(xssl) from xsqkb

where year(xsrq)<2010

where lb in( ) --自连接

select distinct a.cpmc,a.lb,a.cd

from cpb a join cpb b on a.lb=b.lb and a.cd<>b.cd 题型:多产地同类型

步骤:1、要显示的内容,是否涉及不同的表

2、如果只是一张表,那最好用自连接,连接条件是类别相同,产地不同, 3、显示的时候需去重

或者也可以用 having count(distinct cd)>1的子查询来做。

--3、查询只销售过\电冰箱\类产品的销售人员信息。 select * from xsryb where zgh in ( )

题型:只有型

步骤:1、要显示的字段 2、分析有交集

--4、查询销售过\创新牌音响\的销售人员的销售记录次数及平均销售数量,要求只列出销售记录次数大于2次且平均销售数量大于2件的情况 select zgh,count(*),avg(xssl) from xsqkb where zgh in ( )

group by zgh

having count(*)>2 and avg(xssl)>2 题型:多条件型。Having设条件

select a.zgh

from xsqkb a join cpb b on a.cph=b.cph where b.cpmc='创新牌音响' select zgh

from xsqkb a join cpb b on a.cph=b.cph where b.lb='电冰箱' select zgh

from xsqkb a join cpb b on a.cph=b.cph where b.lb<>'电冰箱' select lb from cpb group by lb

having count(distinct cd)>1

) and zgh not in (

--5、查询2001年每个月每个产品的销售总数量,列出月份、产品号和销售总数量,结果先按月份升序排序,再按销售数量降序排序 select month(xsrq),cph,sum(xssl) from xsqkb

where year(xsrq)=2001 group by month(xsrq),cph

order by month(xsrq) asc,sum(xssl) desc 题型:常规

--6、查询销售次数大于平均销售次数的人员信息。 select * from xsryb where zgh in ( )

题型:平均型,以职工号作为连接的子查询

--7、列出\张晓峰\没有销售过的产品名称及价格信息(不能多表连接查询) select cpmc,jg from cpb

where cph not in( )

题型:没有型。分A和B,

--8、查询销售过所有产品的销售人员的职工号 select zgh from xsqkb group by zgh

having count(distinct cph)=( )

题型:所有型。待分析

--9、查询被卖出次数最少的前10%的产品(包括没有被买出过的产品,包括并列的情况),列出

select count(cph) from cpb select b.cph

from xsryb a join xsqkb b on a.zgh=b.zgh where a.xm='张晓峰' select zgh from xsqkb group by zgh having count(*)>( )

select count(*)/count( distinct zgh) from xsryb

产品名称 select cpmc from cpb where cph in ( ) --讨论

select top 10 percent with ties a.CPMC from CPB a left join XSQKB b on a.CPH=b.CPH group by a.CPH,a.CPMC order by COUNT(a.cph) 题型:排序型。左连接或子查询

--10、查询产品表按价格降序排序后排在第6~10位的产品信息 select top 5 * from cpb

where cph not in ( )

order by jg desc

题型:排序型。取中间段显示

--11、将销售人员表中邮政编码列的类型修改为统一编码定长字符型,6位长,不能为空 alter table xsryb

alter column yzbm nchar(6) not null 题型:常型:记住列类型改变用 column

--12、根据产品销售数量对产品价格进行调整。产品销量为0,不调整;产品销量小于5,产品价格下调5%;产品销量在5到10之间,产品价格下调10%;产品销量大于10,产品价格下调15%。 --子查询 update cpb set jg=jg*(

select case

when sum(xssl) is null then 1 when sum(xssl)<5 then 0.95

when sum(xssl) between 5 and 10 then 0.9 when sum(xssl)>10 then 0.85

select top 5 cph from cpb

order by jg desc

select top 10 percent with ties a.cph from cpb a left join xsqkb b on a.cph=b.cph group by a.cph order by count(b.cph)

end

from cpb a left join xsqkb b on a.cph=b.cph where a.cph=cpb.cph group by a.cph

)

--多表连接 create view v4 as

select a.cph,tf= case end

from cpb a left join xsqkb b on a.cph=b.cph group by a.cph

update cpb set jg=jg*b.tf

from cpb a join v4 b on a.cph=b.cph

when sum(xssl) is null then 1 when sum(xssl)<5 then 0.95

when sum(xssl) between 5 and 10 then 0.9 when sum(xssl)>10 then 0.85

1. 查询单价在10到20之间、印刷数量大于5000的“外语”类图书的书名、单价和印刷数量。 select SM,dj,YSSL from TSB

where dj between 10 and 20 and YSSL > 5000 and lb = '外语' 题型:常型:1表3条件

2. 查询店名为“王府井书店”的进书情况,列出图书的书名、进书数量及进书日期 select SM, jYSSL, JSRQ from TSB a join JSB b on a.isbn = b.isbn join SDB c on b.SDBh = c.SDBh where SDDM = '王府井书店' 题型:常型:多表1条件

3.查询地址在“海淀区”的各书店2001年1月1日以后的详细进书情况,列出书店的名称、 每次进书的书名、进书日期和进书数量,要求查询结果按每次进书的数量从多到少的顺序排列。 select SDDM, SM, JSRQ, jYSSL

from TSB a join JSB b on a.isbn = b.isbn join SDB c on b.SDBh = c.SDBh

where DZ like '%海淀区%' and JSRQ>='2001/1/1' order by jYSSL desc 题型:常型:多表2条件排序

4. 查询哪些类别的图书在“王府井书店”从没有进过,列出图书的类别。 select distinct lb from TSB where lb not in(


SQL重要的常用查询语句(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:006编制工程量清单复习题(一)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: