电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

DB2比較常用與實用sql語句總結

瀏覽:146日期:2023-03-24 09:22:32
1、查找員工的編號、姓名、部門和出生日期,如果出生日期為空值,顯示日期不詳,并按部門排序輸出,日期格式為yyyy-mm-dd。
復制代碼 代碼如下:
select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),"日期不詳") birthday
from employee
order by dept



  2、查找與喻自強在同一個單位的員工姓名、性別、部門和職稱
復制代碼 代碼如下:
select emp_no,emp_name,dept,title
from employee
where emp_name<>"喻自強" and dept in
(select dept from employee
where emp_name="喻自強")


  3、按部門進行匯總,統計每個部門的總工資
復制代碼 代碼如下:
select dept,sum(salary)
from employee
group by dept


  4、查找商品名稱為14寸顯示器商品的銷售情況,顯示該商品的編號、銷售數量、單價和金額
復制代碼 代碼如下:
select a.prod_id,qty,unit_price,unit_price*qty totprice
from sale_item a,product b
where a.prod_id=b.prod_id and prod_name="14寸顯示器"


  5、在銷售明細表中按產品編號進行匯總,統計每種產品的銷售數量和金額
復制代碼 代碼如下:
select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice
from sale_item
group by prod_id



  6、使用convert函數按客戶編號統計每個客戶1996年的訂單總金額
復制代碼 代碼如下:
select cust_id,sum(tot_amt) totprice
from sales
where convert(char(4),order_date,120)="1996"
group by cust_id


  7、查找有銷售記錄的客戶編號、名稱和訂單總額
復制代碼 代碼如下:
select a.cust_id,cust_name,sum(tot_amt) totprice
from customer a,sales b
where a.cust_id=b.cust_id
group by a.cust_id,cust_name

  8、查找在1997年中有銷售記錄的客戶編號、名稱和訂單總額
復制代碼 代碼如下:
select a.cust_id,cust_name,sum(tot_amt) totprice
from customer a,sales b
where a.cust_id=b.cust_id and convert(char(4),order_date,120)="1997"
group by a.cust_id,cust_name

  9、查找一次銷售最大的銷售記錄
復制代碼 代碼如下:
select order_no,cust_id,sale_id,tot_amt
from sales
where tot_amt=
(select max(tot_amt)
from sales)

  10、查找至少有3次銷售的業務員名單和銷售日期
復制代碼 代碼如下:
select emp_name,order_date
from employee a,sales b
where emp_no=sale_id and a.emp_no in
(select sale_id
from sales
group by sale_id
having count(*)>=3)
order by emp_name


  11、用存在量詞查找沒有訂貨記錄的客戶名稱
復制代碼 代碼如下:
select cust_name
from customer a
where not exists
(select *
from sales b
where a.cust_id=b.cust_id)


  12、使用左外連接查找每個客戶的客戶編號、名稱、訂貨日期、訂單金額訂貨日期不要顯示時間,日期格式為yyyy-mm-dd按客戶編號排序,同一客戶再按訂單降序排序輸出
復制代碼 代碼如下:
select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
from customer a left outer join sales b on a.cust_id=b.cust_id
order by a.cust_id,tot_amt desc


  13、查找16M DRAM的銷售情況,要求顯示相應的銷售員的姓名、性別,銷售日期、銷售數量和金額,其中性別用男、女表示
復制代碼 代碼如下:
select emp_name 姓名, 性別= case a.sex when "m" then "男"
when "f" then "女"
else "未"
end,
銷售日期= isnull(convert(char(10),c.order_date,120),"日期不詳"),
qty 數量, qty*unit_price as 金額
from employee a, sales b, sale_item c,product d
where d.prod_name="16M DRAM" and d.prod_id=c.prod_id and
a.emp_no=b.sale_id and b.order_no=c.order_no


  14、查找每個人的銷售記錄,要求顯示銷售員的編號、姓名、性別、產品名稱、數量、單價、金額和銷售日期
復制代碼 代碼如下:
select emp_no 編號,emp_name 姓名, 性別= case a.sex when "m" then "男"
when "f" then "女"
else "未"
end,
prod_name 產品名稱,銷售日期= isnull(convert(char(10),c.order_date,120),"日期不詳"),
qty 數量, qty*unit_price as 金額
from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d
where d.prod_id=c.prod_id and b.order_no=c.order_no



  15、查找銷售金額最大的客戶名稱和總貨款
復制代碼 代碼如下:
select cust_name,d.cust_sum
from customer a,
(select cust_id,cust_sum
from (select cust_id, sum(tot_amt) as cust_sum
from sales
group by cust_id ) b
where b.cust_sum =
( select max(cust_sum)
from (select cust_id, sum(tot_amt) as cust_sum
from sales
group by cust_id ) c )
) d
where a.cust_id=d.cust_id


16、查找銷售總額少于1000元的銷售員編號、姓名和銷售額
復制代碼 代碼如下:
select emp_no,emp_name,d.sale_sum
from employee a,
(select sale_id,sale_sum
from (select sale_id, sum(tot_amt) as sale_sum
from sales
group by sale_id ) b
where b.sale_sum <1000
) d
where a.emp_no=d.sale_id


  17、查找至少銷售了3種商品的客戶編號、客戶名稱、商品編號、商品名稱、數量和金額
復制代碼 代碼如下:
select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price
from customer a, product b, sales c, sale_item d
where a.cust_id=c.cust_id and d.prod_id=b.prod_id and
c.order_no=d.order_no and a.cust_id in (
select cust_id
from (select cust_id,count(distinct prod_id) prodid
from (select cust_id,prod_id
from sales e,sale_item f
where e.order_no=f.order_no) g
group by cust_id
having count(distinct prod_id)>=3) h )


  18、查找至少與世界技術開發公司銷售相同的客戶編號、名稱和商品編號、商品名稱、數量和金額
復制代碼 代碼如下:
select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price
from customer a, product b, sales c, sale_item d
where a.cust_id=c.cust_id and d.prod_id=b.prod_id and
c.order_no=d.order_no and not exists
(select f.*
from customer x ,sales e, sale_item f
where cust_name="世界技術開發公司" and x.cust_id=e.cust_id and
e.order_no=f.order_no and not exists
( select g.*
from sale_item g, sales h
where g.prod_id = f.prod_id and g.order_no=h.order_no and
h.cust_id=a.cust_id)
)

  19、查找表中所有姓劉的職工的工號,部門,薪水
復制代碼 代碼如下:
select emp_no,emp_name,dept,salary
from employee
where emp_name like "劉%"


  20、查找所有定單金額高于2000的所有客戶編號
復制代碼 代碼如下:
select cust_id
from sales
where tot_amt>2000


  21、統計表中員工的薪水在4000-6000之間的人數
復制代碼 代碼如下:
select count(*)as 人數
from employee
where salary between 4000 and 6000


  22、查詢表中的同一部門的職工的平均工資,但只查詢"住址"是"上海市"的員工
復制代碼 代碼如下:
select avg(salary) avg_sal,dept
from employee
where addr like "上海市%"
group by dept


  23、將表中住址為"上海市"的員工住址改為"北京市"
復制代碼 代碼如下:
update employee
set addr like "北京市"
where addr like "上海市"


  24、查找業務部或會計部的女員工的基本信息。
復制代碼 代碼如下:
select emp_no,emp_name,dept
from employee
where sex="F"and dept in ("業務","會計")


  25、顯示每種產品的銷售金額總和,并依銷售金額由大到小輸出。
復制代碼 代碼如下:
select prod_id ,sum(qty*unit_price)
from sale_item
group by prod_id
order by sum(qty*unit_price) desc


26、選取編號界于"C0001"和"C0004"的客戶編號、客戶名稱、客戶地址。
復制代碼 代碼如下:
select CUST_ID,cust_name,addr
from customer
where cust_id between "C0001" AND "C0004"

  27、計算出一共銷售了幾種產品。
復制代碼 代碼如下:
select count(distinct prod_id) as "共銷售產品數"
from sale_item

  28、將業務部員工的薪水上調3%。
復制代碼 代碼如下:
update employee
set salary=salary*1.03
where dept="業務"

  29、由employee表中查找出薪水最低的員工信息。
復制代碼 代碼如下:
select *
from employee
where salary=
(select min(salary )
from employee )

  30、使用join查詢客戶姓名為"客戶丙"所購貨物的"客戶名稱","定單金額","定貨日期","電話號碼"
復制代碼 代碼如下:
select a.cust_id,b.tot_amt,b.order_date,a.tel_no
from customer a join sales b
on a.cust_id=b.cust_id and cust_name like "客戶丙"


  31、由sales表中查找出訂單金額大于"E0013業務員在1996/10/15這天所接每一張訂單的金額"的所有訂單。
復制代碼 代碼如下:
select *
from sales
where tot_amt>all
(select tot_amt
from sales
where sale_id="E0013"and order_date="1996/10/15")
order by tot_amt


  32、計算"P0001"產品的平均銷售單價
復制代碼 代碼如下:
select avg(unit_price)
from sale_item
where prod_id="P0001"


  33、找出公司女員工所接的定單
復制代碼 代碼如下:
select sale_id,tot_amt
from sales
where sale_id in
(select sale_id from employee
where sex="F")



  34、找出同一天進入公司服務的員工
復制代碼 代碼如下:
select a.emp_no,a.emp_name,a.date_hired
from employee a
join employee b
on (a.emp_no!=b.emp_no and a.date_hired=b.date_hired)
order by a.date_hired


  35、找出目前業績超過232000元的員工編號和姓名。
復制代碼 代碼如下:
select emp_no,emp_name
from employee
where emp_no in
(select sale_id
from sales
group by sale_id
having sum(tot_amt)<232000)
標簽: DB2
相關文章:
主站蜘蛛池模板: Duoguan 夺冠集团| 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | 耐腐蚀泵,耐腐蚀真空泵,玻璃钢真空泵-淄博华舜耐腐蚀真空泵有限公司 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 威海防火彩钢板,威海岩棉复合板,威海彩钢瓦-文登区九龙岩棉复合板厂 | 好物生环保网、环保论坛 - 环保人的学习交流平台 | 火锅底料批发-串串香技术培训[川禾川调官网] | 德国进口电锅炉_商用电热水器_壁挂炉_电采暖器_电热锅炉[德国宝] | 合肥汽车充电桩_安徽充电桩_电动交流充电桩厂家_安徽科帝新能源科技有限公司 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 预制围墙_工程预制围墙_天津市瑞通建筑材料有限公司 | 福建珂朗雅装饰材料有限公司「官方网站」| 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 臭氧老化试验箱,高低温试验箱,恒温恒湿试验箱,防水试验设备-苏州亚诺天下仪器有限公司 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 无线对讲-无线对讲系统解决方案-重庆畅博通信 | 选宝石船-陆地水上开采「精选」色选机械设备-青州冠诚重工机械有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 海峰资讯 - 专注装饰公司营销型网站建设和网络营销培训 | 期货软件-专业期货分析软件下载-云智赢 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 起好名字_取个好名字_好名网免费取好名在线打分 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 液压扳手-高品质液压扳手供应商 - 液压扳手, 液压扳手供应商, 德国进口液压拉马 | 安徽免检低氮锅炉_合肥燃油锅炉_安徽蒸汽发生器_合肥燃气锅炉-合肥扬诺锅炉有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 福建成考网-福建成人高考网| 北京京云律师事务所| 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 镀锌钢格栅_热镀锌格栅板_钢格栅板_热镀锌钢格板-安平县昊泽丝网制品有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 三轴曲线机-端子插拔力试验机|华杰仪器 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 |