db2比较常用与实用sql语句总结1Word格式.docx
- 文档编号:7313874
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:8
- 大小:16.95KB
db2比较常用与实用sql语句总结1Word格式.docx
《db2比较常用与实用sql语句总结1Word格式.docx》由会员分享,可在线阅读,更多相关《db2比较常用与实用sql语句总结1Word格式.docx(8页珍藏版)》请在冰点文库上搜索。
selecta.prod_id,qty,unit_price,unit_price*qtytotprice
fromsale_itema,productb
wherea.prod_id=b.prod_idandprod_name='
14寸显示器'
5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额
selectprod_id,sum(qty)totqty,sum(qty*unit_price)totprice
fromsale_item
groupbyprod_id
6、使用convert函数按客户编号统计每个客户1996年的订单总金额
selectcust_id,sum(tot_amt)totprice
fromsales
whereconvert(char(4),order_date,120)='
1996'
groupbycust_id
7、查找有销售记录的客户编号、名称和订单总额
selecta.cust_id,cust_name,sum(tot_amt)totprice
fromcustomera,salesb
wherea.cust_id=b.cust_id
groupbya.cust_id,cust_name
8、查找在1997年中有销售记录的客户编号、名称和订单总额
wherea.cust_id=b.cust_idandconvert(char(4),order_date,120)='
1997'
9、查找一次销售最大的销售记录
selectorder_no,cust_id,sale_id,tot_amt
wheretot_amt=
(selectmax(tot_amt)
fromsales)
10、查找至少有3次销售的业务员名单和销售日期
selectemp_name,order_date
fromemployeea,salesb
whereemp_no=sale_idanda.emp_noin
(selectsale_id
groupbysale_id
havingcount(*)>
=3)
orderbyemp_name
11、用存在量词查找没有订货记录的客户名称
selectcust_name
fromcustomera
wherenotexists
(select*
fromsalesb
wherea.cust_id=b.cust_id)
12、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额订货日期不要显示时间,日期格式为yyyy-mm-dd按客户编号排序,同一客户再按订单降序排序输出
selecta.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
fromcustomeraleftouterjoinsalesbona.cust_id=b.cust_id
orderbya.cust_id,tot_amtdesc
13、查找16mdram的销售情况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用男、女表示
selectemp_name姓名,性别=casea.sexwhen'
m'
then'
男'
when'
f'
女'
else'
未'
end,
销售日期=isnull(convert(char(10),c.order_date,120),'
),
qty数量,qty*unit_priceas金额
fromemployeea,salesb,sale_itemc,productd
whered.prod_name='
16mdram'
andd.prod_id=c.prod_idand
a.emp_no=b.sale_idandb.order_no=c.order_no
14、查找每个人的销售记录,要求显示销售员的编号、姓名、性别、产品名称、数量、单价、金额和销售日期
selectemp_no编号,emp_name姓名,性别=casea.sexwhen'
prod_name产品名称,销售日期=isnull(convert(char(10),c.order_date,120),'
fromemployeealeftouterjoinsalesbona.emp_no=b.sale_id,sale_itemc,productd
whered.prod_id=c.prod_idandb.order_no=c.order_no
15、查找销售金额最大的客户名称和总货款
selectcust_name,d.cust_sum
fromcustomera,
(selectcust_id,cust_sum
from(selectcust_id,sum(tot_amt)ascust_sum
groupbycust_id)b
whereb.cust_sum=
(selectmax(cust_sum)
groupbycust_id)c)
)d
wherea.cust_id=d.cust_id
16、查找销售总额少于1000元的销售员编号、姓名和销售额
selectemp_no,emp_name,d.sale_sum
fromemployeea,
(selectsale_id,sale_sum
from(selectsale_id,sum(tot_amt)assale_sum
groupbysale_id)b
whereb.sale_sum )d
wherea.emp_no=d.sale_id
17、查找至少销售了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额
selecta.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price
fromcustomera,productb,salesc,sale_itemd
wherea.cust_id=c.cust_idandd.prod_id=b.prod_idand
c.order_no=d.order_noanda.cust_idin(
selectcust_id
from(selectcust_id,count(distinctprod_id)prodid
from(selectcust_id,prod_id
fromsalese,sale_itemf
wheree.order_no=f.order_no)g
havingcount(distinctprod_id)>
=3)h)
18、查找至少与世界技术开发公司销售相同的客户编号、名称和商品编号、商品名称、数量和金额
selecta.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price
c.order_no=d.order_noandnotexists
(selectf.*
fromcustomerx,salese,sale_itemf
wherecust_name='
世界技术开发公司'
andx.cust_id=e.cust_idand
e.order_no=f.order_noandnotexists
(selectg.*
fromsale_itemg,salesh
whereg.prod_id=f.prod_idandg.order_no=h.order_noand
h.cust_id=a.cust_id)
)
19、查找表中所有姓刘的职工的工号,部门,薪水
selectemp_no,emp_name,dept,salary
whereemp_namelike'
刘%'
20、查找所有定单金额高于20XX的所有客户编号
wheretot_amt>
20XX
21、统计表中员工的薪水在4000-6000之间的人数
selectcount(*)as人数
wheresalarybetween4000and6000
22、查询表中的同一部门的职工的平均工资,但只查询住址是上海市的员工
selectavg(salary)avg_sal,dept
whereaddrlike'
上海市%'
23、将表中住址为上海市的员工住址改为北京市
updateemployee
setaddrlike'
北京市'
上海市'
24、查找业务部或会计部的女员工的基本信息。
selectemp_no,emp_name,dept
wheresex='
anddeptin('
业务'
'
会计'
25、显示每种产品的销售金额总和,并依销售金额由大到小输出。
selectprod_id,sum(qty*unit_price)
orderbysum(qty*unit_price)desc
26、选取编号界于'
c0001'
和'
c0004'
的客户编号、客户名称、客户地址。
selectcust_id,cust_name,addr
fromcustomer
wherecust_idbetween'
and'
27、计算出一共销售了几种产品。
selectcount(distinctprod_id)as'
共销售产品数'
28、将业务部员工的薪水上调3%。
setsalary=salary*1.03
wheredept='
29、由employee表中查找出薪水最低的员工信息。
select*
wheresalary=
(selectmin(salary)
fromemployee)
30、使用join查询客户姓名为客户丙所购货物的客户名称,定单金额,定货日期,电话号码
selecta.cust_id,b.tot_amt,b.order_date,a.tel_no
fromcustomerajoinsalesb
ona.cust_id=b.cust_idandcust_namelike'
客户丙'
31、由sales表中查找出订单金额大于e0013业务员在1996/10/15这天所接每一张订单的金额的所有订单。
all
(selecttot_amt
wheresale_id='
e0013'
andorder_date='
1996/10/15'
orderbytot_amt
32、计算'
p0001'
产品的平均销售单价
selectavg(unit_price)
whereprod_id='
33、找出公司女员工所接的定单
selectsale_id,tot_amt
wheresale_idin
(selectsale_idfromemployee
34、找出同一天进入公司服务的员工
selecta.emp_no,a.emp_name,a.date_hired
fromemployeea
joinemployeeb
on(a.emp_no!
=b.emp_noanda.date_hired=b.date_hired)
orderbya.date_hired
35、找出目前业绩超过2320XX元的员工编号和姓名。
selectemp_no,emp_name
whereemp_noin
havingsum(tot_amt)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- db2 比较 常用 实用 sql 语句 总结