当前位置: 首页 > 科技观察

DB2中常用和实用的SQL语句汇总

时间:2023-03-12 21:33:07 科技观察

1.查找员工的编号、姓名、部门和出生日期。如果出生日期为空,则显示日期未知,按部门排序输出。日期格式为yyyy-mm-dd。复制代码代码如下:selectemp_no,emp_name,dept,isnull(convert(char(10),birthday,120),'dateisunknown')birthdayfromemployeeorderbydept  2。查找与于自强同单位的员工姓名、性别、部门、职称复制代码代码如下:selectemp_no,emp_name,dept,titlefromemployeewhereemp_name<>'于自强'anddeptin(selectdeptfromemployeewhereemp_name='于自强')  3.按部门汇总统计各部门的工资总额。复制代码代码如下:selectdept,sum(salary)fromemployeegroupbydept  4。查找产品名称为14寸显示器的产品销售状态,显示产品编号、销售数量、单价、金额复制code代码如下:selecta.prod_id,qty,unit_price,unit_price*qtytotpricefromsale_itema,productbwherea.prod_id=b.prod_idandprod_name='14-inchdisplay'  5、Onsale明细表按产品编号汇总,统计每个产品的销售数量和金额。复制code代码如下:selectprod_id,sum(qty)totqty,sum(qty*unit_price)totpricefromsale_itemgroupbyprod_id  6、使用convert函数,根据客户编号统计每个客户在1996年的订单总金额复制code代码如下:selectcust_id,sum(tot_amt)totpricefromsaleswhereconvert(char(4),order_date,120)='1996'groupbycust_id  7.通过销售记录查找客户编号、姓名和总订单金额。复制code代码如下:selecta.cust_id,cust_name,sum(tot_amt)totpricefromcustomera,salesbwherea.cust_id=b.cust_idgroupbya.cust_id,cust_name  8。查找1997年有销售记录的客户编号、姓名和总订单金额,复制code代码如下:selecta.cust_id,cust_name,sum(tot_amt)totpricefromcustomera,salesbwherea.cust_id=b.cust_idandconvert(char(4),order_date,120)='1997'groupbya.cust_id,cust_name  9。查找销售额最大的销售记录。copycode代码如下:selectorder_no,cust_id,sale_id,tot_amtfromsales其中tot_amt=(selectmax(tot_amt)fromsales)  10。查找至少有3次销售的销售人员名单和销售日期。复制code代码如下:selectemp_name,order_datefromemployeea,salesbwhereemp_no=sale_idanda.emp_noin(selectsale_idfromsalesgroupbysale_idhavingcount(*)>=3)orderbyemp_name  11.使用存在量词搜索没有订单记录的客户姓名。复制code代码如下:selectcust_namefromcustomerawherenotexists(select*fromsalesbwherea.cust_id=b.cust_id)  12。使用左外连接查找每个客户客户编号、姓名、订单日期、订单金额和订单日期不显示时间。日期格式为yyyy-mm-dd,按客户编号排序,同一客户按顺序降序排列。输出复制代码代码如下:selecta.cust_id,cust_name,convert(char(10),order_date,120),tot_amtfromcustomeraleftouterjoinsalesbona.cust_id=b.cust_idorderbya.cust_id,tot_amtdesc  13.查找16MDRAM的销售状态,要求显示对应的销售人员姓名、性别、销售日期、销售数量和金额,其中性别用男、女表示。复制代码代码如下:selectemp_namename,gender=casea.sexwhen'm'then'male'when'f'then'female'else'not'end,salesdate=isnull(convert(char(10),c.order_date,120),'dateunknown'),qtyquantity,qty*unit_priceasamountfromemployeea,salesb,sale_itemc,productdwhered.prod_name='16MDRAM'andd.prod_id=c.prod_idanda.emp_no=b.sale_idandb.order_no=c.order_no  14、找出每个人的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期。复制code代码如下:selectemp_nonumber,emp_namename,gender=casea.sexwhen'm'then'male'when'f'then'female'else'not'end,prod_nameproductname,salesdate=isnull(convert(char(10),c.order_date,120),'dateunknown'),qtyquantity,qty*unit_priceasamountfromemployeealeftouterjoinsalesbona.emp_no=b.sale_id,sale_itemc,产品dwhered.prod_id=c.prod_id和b.order_no=c.order_no  15.找到销售额最大的客户名称和copycode代码如下:selectcust_name,d.cust_sumfromcustomera,(selectcust_id,cust_sumfrom(selectcust_id,sum(tot_amt)ascust_sumfromsalesgroupbycust_id)bwhereb.cust_sum=(selectmax(cust_sum)from(selectcust_id,sum(tot_amt)ascust_sumfromsalesgroupbycust_id)c))dwherea.cust_id=d.cust_id16,求总销售额的销售员的人数、姓名和销售额金额小于1000元复制code代码如下:selectemp_no,emp_name,d.sale_sumfromemployeea,(selectsale_id,sale_sumfrom(selectsale_id,sum(tot_amt)assale_sumfromsalesgroupbysale_id)bwhereb.sale_sum<1000)dwherea.emp_no=d.sale_id  17.找出至少销售过3种产品的客户编号、客户名称、产品编号、产品名称、数量和金额。复制code代码如下:selecta.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_pricefromcustomera,productb,salesc,sale_itemdwherea.cust_id=c.cust_idandd.prod_id=b.prod_idandc.order_no=d.order_noanda.cust_idin(selectcust_idfrom(selectcust_id,count(distinctprod_id)prodidfrom(selectcust_id,prod_idfromsalese,sale_itemfwheree.order_no=f.order_no)ggroupbycust_idhavingcount(distinctprod_id)>=3)h)  18.找出至少与世界科技发展公司销售相同的客户编号、名称及产品编号、产品名称、数量及金额。复制code代码如下:selecta.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_pricefromcustomera,productb,salesc,sale_itemdwherea.cust_id=c.cust_idandd.prod_id=b.prod_idandc.order_no=d.order_noandnotexists(selectf.*fromcustomerx,salese,sale_itemfwherecust_name='世界科技发展公司'andx.cust_id=e.cust_idande.order_no=f.order_no并且不存在(selectg.*fromsale_itemg,saleshwhereg.prod_id=f.prod_idandg.order_no=h.order_noandh.cust_id=a.cust_id))  19。查找表中所有刘姓员工的工号、部门、工资。复制代码如下:selectemp_no,emp_name,dept,salaryfromemployeewhereemp_namelike'Liu%'  20。找出所有订单金额大于2000的客户编号,复制代码如下:selectcust_idfromsaleswheretot_amt>2000  21。统计表中工资在4000-6000之间的员工人数如下。复制代码代码如下:selectcount(*)asnumberofpeoplefromemployeewheresalarybetween4000and6000  22、查询表中同一部门员工的平均工资,但只查询“地址”为“上海市”的员工复制代码代码如下:selectavg(salary)avg_sal,deptfromemployeewhereaddrlike'ShanghaiCity%'groupbydept  23.将表中地址为“上海”的员工地址修改为“北京”复制code代码如下:updateemployeesetaddrlike'Beijing'whereaddrlike'Shanghai'  24。查找业务部或会计部女员工基本信息。复制代码代码如下:selectemp_no,emp_name,deptfromemployeewheresex='F'anddeptin('business','accounting')  25。显示每个产品的销售额总和,销售额从大到小输出。复制代码代码如下:selectprod_id,sum(qty*unit_price)fromsale_itemgroupbyprod_idorderbysum(qty*unit_price)desc26,selectcustomernumber,customernameandcustomeraddress'c0001'and'C0004'。复制代码代码如下:selectCUST_ID,cust_name,addrfromcustomerwherecust_idbetween'C0001'AND'C0004'  27.计算已售出多少产品。复制代码代码如下:selectcount(distinctprod_id)as'totalnumberofproductssold'fromsale_item  28。提高业务部门员工工资3%。复制代码代码如下:updateemployeesetsalary=salary*1.03wheredept='business'  29.从employee表中找出工资最低的员工信息。复制代码代码如下:select*fromemployeewheresalary=(selectmin(salary)fromemployee)  30。使用join查询“客户名称”、“订单金额”、“OrderDate”、“PhoneNumber”复制code代码如下:selecta.cust_id,b.tot_amt,b.order_date,a.tel_nofromcustomerajoinsalesbona.cust_id=b.cust_idandcust_namelike'CustomerC'  31。从sales表中找出所有订单金额大于“E0013业务员在1996/10/15收到的每笔订单金额”的订单。复制代码代码如下:select*fromsaleswheretot_amt>all(selecttot_amtfromsaleswheresale_id='E0013'andorder_date='1996/10/15')orderbytot_amt  32,计算平均销售单价'P0001'productcopycode代码如下:selectavg(unit_price)fromsale_itemwhereprod_id='P0001'  33,找出公司女员工收到的订单Copycode代码如下:selectsale_id,tot_amtfromsaleswheresale_idin(selectsale_idfromemployeewheresex='F')  34、找出当天进入公司服务的员工,复制代码如下:selecta.emp_no,a.emp_name,a.date_hiredfromemployeeajoinemployeebon(a.emp_no!=b.emp_noanda.date_hired=b.date_hired)由a.date_hired  35订购。查出当前业绩超过23.2万元的员工ID和姓名。复制代码代码如下:selectemp_no,emp_namefromemployeewhereemp_noin(selectsale_idfromsalesgroupbysale_idhavingsum(tot_amt)<232000)