千里之行,始于足下让知识带有温度。第第2页/共2页精品文档推荐数据库SQL查询语句实验报告试验一容易查询
在订单数据库中完成如下的查询
(1)查询全部业务部门的员工姓名、职称、薪水
命令:
selectemployeeName2612,headShip2612,salary2612
fromEmployee2612
wheredepartment2612='业务科'
结果:
(2)查询名字中含有“有限”的客户姓名和所在地。
selectCustomerName2612,address2612
fromCustomer2612
whereCustomerName2612like'%有限%'
(3)查询出姓“王”并且姓名的最后一个字为“成”的员工。
select*
whereemployeeName2612like'王%成'
(4)查询住址中含有上海或南昌的女员工,并显示其姓名、所属部门、职称、住址,其中性别用“男”和“女”显示。
selectemployeeName2612,department2612,headShip2612,address2612,
casesex2612
when'M'then'男'
when'F'then'女'
endas性别
whereaddress2612like'%上海%'oraddress2612like'%南昌%'andsex2612='F'结果:
(5)在表sales中挑出销售金额大于等于10000元的订单。
SELECTorderNo2612,sum(quantity2612*price2612)astotal
FROMOrderDetail2612
GROUPBYorderNo2612
HAVINGsum(quantity2612*price2612)>10000
(6)选取订单金额最高的前10%的订单数据。
selectTOP10PERCENTorderNo2612,sum(quantity2612*price2612)astotalfromOrderDetail2612
groupbyorderNo2612
ORDERBYtotaldesc
(7)查询出职务为“职员”或职务为“科长”的女员工的信息。
whereheadShip2612in('科长','职员')andsex2612='F'
(8)查找定单金额高于8000的全部客户编号。
selectCustomerNo2612
fromOrderDetail2612asa,OrderMaster2612asb
wherea.orderNo2612=b.orderNo2612
groupbyCustomerNo2612
havingsum(quantity2612*price2612)>8000
(9)选取编号界于“C20220001”和“C20220004”的客户编号、客户名称、客户地址。命令:
SELECTCustomerNo2612,CustomerName2612,address2612
FROMCustomer2612
WHERECustomerNo2612BETWEEN'C20220001'AND'C20220004'
(11)找出同一天进入公司服务的员工。
selecthireDate2612,employeeName2612
fromEmployee2612asa
whereexists
(select*fromEmployee2612asb
wherea.hireDate2612=b.hireDate2612anda.employeeNo2612!=b.employeeNo2612)
groupbyhireDate2612,employeeName2612
(12)在订单主表中查询订单金额大于“E2022002业务员在2022-1-9这天所接的任一张订单的金额”的全部订单信息。
SELECT
a.orderNo2612,CustomerNO2612,salerNo2612,orderDate2612,invoiceNo2612,sum(quantity2612*price2612)orderSum
FROMOrderDetail2612a,OrderMaster2612b
WHEREa.orderNo2612=b.orderNo2612
GROUPBYa.orderNo2612,CustomerNO2612,salerNo2612,orderDate2612,invoiceNo2612
HAVINGsum(quantity2612*price2612)>ALL
(SELECTsum(quantity2612*price2612)
WHEREa.orderNo2612=b.orderNo2612andsalerNo2612='E2022002'andorderDate2612='2022-01-0900:00:00.000'
GROUPBYa.orderNo2612)
(13)查询既订购了“52倍速光驱”商品,又订购了“17寸显示器”商品的客户编号、订单编号和订单金额。
selectb.CustomerNo2612,a.orderNo2612,sum(quantity2612*price2612)astotal
fromOrderDetail2612asa,OrderMaster2612asb,Product2612asc,
(selectd.orderNo2612fromOrderDetail2612asd,Product2612asewhereProductName2612='17寸显示器'andd.ProductNo2612=e.ProductNo2612)asf
wherec.ProductName2612='52倍速光驱'anda.orderNo2612=b.orderNo2612anda.orderNo2612=f.orderNo2612
groupbyb.CustomerNo2612,a.orderNo2612
(14)查找与“陈诗杰”在同一个单位工作的员工姓名、性别、部门和职务。
selecta.employeeName2612,a.sex2612,a.department2612,a.headShip2612
fromEmployee2612asa,(select*fromEmployee2612whereemployeeName2612='陈诗杰')asbwherea.department2612=b.department2612
(15)查询每种商品的商品编号、商品名称、订货数量和订货单价。
selectb.ProductName2612,a.ProductNo2612,a.total,a.price2612
from(
selectsum(quantity2612)astotal,ProductNo2612,price2612
fromOrderDetail2612
groupbyProductNo2612,price2612)asa,Product2612asb
wherea.ProductNo2612=b.ProductNo2612
(16)查询单价高于400元的商品编号、商品名称、订货数量和订货单价。
groupbyProductNo2612,price2612
havingprice2612>400)asa,Product2612asb
(17)分离使用左外衔接、右外衔接、完整外部衔接查询单价高于400元的商品编号、商品名称、订货数量和订货单价,并分析比较检索的结果。
selecta.ProductNo2612,ProductName2612,quantity2612,price2612
fromProduct2612aLEFTOUTERJOINOrderDetail2612bONa.ProductNo2612=b.ProductNo2612
groupbya.ProductNo2612,quantity2612,price2612,ProductName2612
havingprice2612>400
fromProduct2612aRIGHTOUTERJOINOrderDetail2612bONa.ProductNo2612=b.ProductNo2612
fromProduct2612aFULLOUTERJOINOrderDetail2612bONa.ProductNo2612=b.ProductNo2612
结果:(18)查找每个员工的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期,其中性别使用“男”和“女”表示,日期使用“yyyy-mm-dd”格式显示。
SELECTemployeeNo2612,employeeName2612,
casesex2612when'F'then'女'
endas性别,ProductName2612,quantity2612,price2612,quantity2612*price2612orderSum,ISNULL(convert(char(10),orderDate2612,120),'')日期
FROMEmployee2612a,OrderMaster2612b,OrderDetail2612c,Product2612d
WHEREemployeeNo2612=salerNo2612andb.orderNo2612=c.orderNo2612andc.ProductNo2612=d.ProductNo2612
(19)查找在2022年3月中有销售记录的客户编号、名称和订单总额。
a.CustomerNo2612,a.CustomerName2612,orderDate2612,sum(quantity2612*price2612)as金额FROMCustomer2612a,OrderMaster2612b,OrderDetail2612c
WHEREa.CustomerNo2612=b.CustomerNo2612andb.orderNo2612=c.orderNo2612andyear(orderDate2612)=2022andmonth(orderDate2612)=3
GROUPBYa.CustomerNo2612,a.CustomerName2612,orderDate2612
(20)使用左外衔接查找每个客户的客户编号、名称、订货日期、订单金额,其中订货日期不要显示时光,日期格式为“yyyy-mm-dd”,按客户编号排序,同一客户再按订单金额降序排序输出。
a.CustomerNo2612,a.CustomerName2612,ISNULL(convert(char(10),orderDate2612,120),'')DateFROMCustomer2612aLEFTJOINOrderMaster2612bONa.CustomerNo2612=
b.CustomerNo2612
ORDERBYa.CustomerNo2612,orderSum2612
(21)查找16MDRAM的销售状况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用“男”、“女”表示。
selectemployeeName2612,
endas性别,orderDate2612,quantity2612,sum(quantity2612*price2612)as金额
fromEmployee2612a,OrderMaster2612b,OrderDetail2612c
whereemployeeNo2612=salerNo2612andb.orderNo2612=c.orderNo2612andProductNo2612in(selectProductNo2612fromProduct2612whereProductName2612='16MDRAM')
groupbyemployeeName2612,sex2612,orderDate2612,quantity2612
(22)查找每个人的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期。
select
employeeNo2612,employeeName2612,sex2612,ProductName2612,quantity2612,price2612,sum(quantity2612*price2612)
asordersum,orderDate2612
fromEmployee2612a,OrderMaster2612b,OrderDetail2612c,Product2612d
whereemployeeNo2612=salerNo2612andb.orderNo2612=c.orderNo2612andc.ProductNo2612=d.ProductNo2612
groupbyemployeeNo2612,employeeName2612,sex2612,ProductName2612,quantity2612,price2612,orderDate2612
SELECTa.CustomerName2612,quantity2612*price2612订单金额,orderDate2612,telephone2612
FROMCustomer2612a,OrderMaster2612b,OrderDetail2612c
WHERECustomerName2612='客户丙'anda.CustomerNo2612=b.CustomerNo2612andb.orderNo2612=c.orderNo2612
(24)找出公司男业务员所接且订单金额超过2000元的订单号及订单金额。
selectb.orderNo2612,sum(quantity2612*price2612)ordersum
wheresex2612='M'anda.employeeNo2612=b.salerNo2612andb.orderNo2612=c.orderNo2612groupbyb.orderNo2612
havingsum(quantity2612*price2612)>2000
selectCustomerName2612,telephone2612,b.orderNo2612,sum(quantity2612*price2612)asordersum
fromCustomer2612a,OrderMaster2612b,OrderDetail2612c
whereaddress2612='上海市'anda.CustomerNo2612=b.CustomerNo2612andb.orderNo2612=c.orderNo2612
groupbyCustomerName2612,telephone2612,b.orderNo2612
试验二题目
首先使用命令:
UPDATEOrderMaster2612SETorderSum2612=total
FROMOrderMaster2612a,
(SELECTorderNo2612,sum(quantity2612*price2612)astotal
GROUPBYorderNo2612)b
更新表OrderMaster2612中orderSum2612的值。
(1)查找有销售记录的客户编号、名称和订单总额。
SELECTa.CustomerNo2612,CustomerName2612,sum(orderSum2612)Sum
FROMCustomer2612a,OrderMaster2612b
WHEREa.CustomerNo2612=b.CustomerNo2612andorderSum2612!=0
GROUPBYa.CustomerNo2612,CustomerName2612
(2)在订单明细表中查询订单金额最高的订单。
SELECT*
FROMOrderMaster2612
WHEREorderSum2612=(SELECTmax(orderSum2612)FROMOrderMaster2612)
(3)查询没有订购商品的客户编号和客户名称。
SELECTa.CustomerNo2612,CustomerName2612,
FROMCustomer2612a
WHERENOTEXISTS(
FROMOrderMaster2612b,OrderDetail2612c
WHEREa.CustomerNo2612=b.CustomerNo2612andb.orderNo2612=c.orderNo2612)GROUPBYa.CustomerNo2612,CustomerName2612
(4)找出至少被订购3次的商品编号、订单编号、订货数量和订货金额,并按订货数量的降序排序输出。
SELECTproductNo2612,orderNo2612,quantity2612,quantity2612*price2612订货金额
WHEREproductNO2612IN(
SELECTproductNo2612
GROUPBYproductNo2612
HAVINGcount(*)>=3)
ORDERBYproductNo2612desc
(5)使用子查询查找16MDRAM的销售状况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用“男”、“女”表示。
SELECTemployeeName2612,
CASEsex2612WHEN'F'THEN'女'
WHEN'M'THEN'男'
ENDAS性别,orderDate2612,quantity2612,sum(quantity2612*price2612)as金额
FROMEmployee2612a,OrderMaster2612b,OrderDetail2612c
WHEREemployeeNo2612=salerNo2612andb.orderNo2612=c.orderNo2612andProductNo2612in
(SELECTProductNo2612FROMProduct2612WHEREProductName2612='16MDRAM')
(6)查询sales表中订单金额最高的订单号及订单金额。
SELECTorderNo2612,orderSum2612
(7)计算出一共销售了几种商品。
SELECTcount(distinctproductNo2612)商品种类
(8)显示OrderDetail表中每种商品的订购金额总和,并且依据销售金额由大到小排序输出。
SELECTproductNo2612,sum(quantity2612*price2612)订购金额
ORDERBY订购金额DESC
(9)查找销售总额少于1000元的销售员编号、姓名和销售额。
SELECTemployeeNo2612,employeeName2612,sum(orderSum2612)销售额
FROMOrderMaster2612aRIGHTOUTERJOINEmployee2612bONsalerNo2612=employeeNo2612
WHEREemployeeNo2612NOTIN(
SELECTsalerNo2612
WHEREsalerNo2612NOTIN(
GROUPBYsalerNo2612
HAVINGsum(orderSum2612)(
SELECTavg(salary2612)
FROMEmployee2612)
GROUPBYemployeeNo2612,employeeName2612,salary2612
(12)计算每一种商品的销售数量、平均销售单价和总销售金额。
SELECTproductNo2612,count(quantity2612)销售数量,avg(price2612)平均销售单价,sum(quantity2612*price2612)总销售额
ORDERBYproductNo2612
(13)查找至少有3次销售的业务员名单和销售日期。
SELECTemployeeName2612,orderDate2612
FROMEmployee2612a,OrderMaster2612b,(
HAVINGcount(*)>=3)c
WHEREemployeeNo2612=b.salerNo2612andb.salerNo2612=c.salerNo2612GROUPBYemployeeName2612,orderDate2612
(14)用存在量词查找没有订货记录的客户名称。
SELECTCustomerName2612
SELECTCustomerNo2612
FROMOrderMaster2612b
WHEREa.CustomerNo2612=b.CustomerNo2612)
(15)查询订单中所订购的商品数量没有超过10个的客户编号和客户名称。
SELECTa.CustomerNo2612,a.CustomerName2612
WHEREEXISTS(
FROMOrderDetail2612b,OrderMaster2612c
WHEREa.CustomerNo2612=c.CustomerNo2612andb.orderNo2612=c.orderNo2612
GROUPBYa.CustomerNo2612
HAVINGsum(quantity2612)8000
(19)显示每种商品的销售金额总和,并依销售金额由大到小输出。命令:
SELECTproductNo2612,sum(price2612*quantity2612)销售金额
ORDERBY销售金额desc
(20)查找销售金额最大的客户名称和总货款。
SELECTtop1CustomerName2612,max(总货款)总货款
FROM(SELECTCustomerNo2612,sum(orderSum2612)总货款
GROUPBYCustomerNo2612)a,OrderMaster2612b,Customer2612c
WHEREa.CustomerNo2612=b.CustomerNo2612andb.CustomerNo2612=c.CustomerNo2612GROUPBYCustomerName2612
ORDERBY总货款desc
(21)查找至少销售了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额。
SELECTa.CustomerNo2612,a.CustomerName2612,ductNo2612,productName2612,quantity2612,quantity2612*price2612金额,orderDate2612FROMCustomer2612a,OrderMaster2612b,OrderDetail2612c,Product2612d
WHEREb.orderNo2612=c.orderNO2612ductNo2612=ductNo2612anda.CustomerNo2612IN(
WHEREorderNo2612IN(
SELECTorderNO2612
HAVINGcount(*)>=3))
ORDERBYa.CustomerNo2612,ductNo2612
(22)找出目前业绩超过232000元的员工编号和姓名。
SELECTemployeeNo2612,employeeName2612
FROMOrderMaster2612,Employee2612
WHEREsalerNo2612=employeeNo2612
GROUPBYemployeeNo2612,employeeName2612
HAVINGsum(orderSum2612)>232000
(23)找出目前销售业绩超过40000元的业务员编号及销售业绩,并按销售业绩从大到小排序。
SELECTemployeeNo2612,sum(orderSum2612)销售额
HAVINGsum(orderSum2612)>40000
ORDERBY销售额desc
(24)求出每位客户的总订购金额,显示出客户号及总订购金额,并按总订购金额降序罗列。