数据库系统原理与设计实验教程实验1实验一简单查询在订单数据库中,完成如下的查询:(1) 查询所有业务部门的员工姓名、职称、薪水。
select employeeName,headShip,salaryfrom employeewhere department='业务科'(2) 查询名字中含有“有限”的客户姓名和所在地。
select CustomerName,addressfrom Customerwhere CustomerName like '%有限%'(3) 查询出姓“王”并且姓名的最后一个字为“成”的员工。
select *from employeewhere employeeName like '王%成'没有结果(4) 查询住址中含有上海或南昌的女员工,并显示其姓名、所属部门、职称、住址,其中性别用“男”和“女”显示。
selectemployeeName,department,headship,address,sex= Case sexwhen 'M'then '男'when 'F'then '女'endfrom employeewhere address like'%上海%' or address like '%南昌%' and sex='F'(5) 在订单明细表OrderDetail中挑出销售金额大于等于10000元的订单。
select orderNofrom OrderDetailgroup by orderNohaving sum(quantity*price)>=10000(6) 选取订单金额最高的前10%的订单数据。
SELECT TOP 10 PERCENT orderNoFROM OrderdetailGROUP BY orderNoORDER BY sum(quantity*price) DESC(7) 查询出职务为“职员”或职务为“科长”的女员工的信息。
select *from employeewhere (headship='职员' or headship='科长') and sex='F'(8) 查找定单金额高于8000的所有客户编号。
1)查询总金额高于8000元的客户编号select CustomerNofrom OrderMaster a,Orderdetail bwhere a.orderNo=b.orderNogroup by CustomerNohaving sum(quantity*price)>80002)查找定单金额高于8000的所有客户编号select CustomerNofrom OrderMasterwhere orderNo in ( select orderNofrom OrderDetailgroup by orderNohavingsum(quantity*price)>8000 )(9) 选取编号界于“C20050001”和“C20050004”的客户编号、客户名称、客户地址。
select CustomerNo,CustomerName,addressfrom Customerwhere CustomerNo between 'C20050001' and 'C20050004'(11) 找出同一天进入公司服务的员工。
Selecta.employeeNo,a.employeeName,b.employeeNo,b.employeeNamefrom Employee a,Employee as bwhere a.employeeNo!=b.employeeNo anda.employeeName>b.employeeNameand (a.hireDate=b.hireDate)(12) 在订单主表中查询订单金额大于“E2005002业务员在2008-1-9这天所接的任一张订单的金额”的所有订单信息。
1)首先计算订单主表的订单金额update OrderMaster set orderSum=totalSumfrom OrderMaster a,(select orderNo,sum(quantity*price) totalSumfrom OrderDetailgroup by orderNo) bwhere a.orderNO=b.orderNo2)SELECT *FROM OrderMasterWHERE orderSum>any(SELECT orderSumFROM OrderMasterWHERE salerNo='E2005002' AND orderDate='20080109' )(13) 查询既订购了“52倍速光驱”商品,又订购了“17寸显示器”商品的客户编号、订单编号和订单金额。
Select customerNo,orderNo,orderSumfrom OrderMasterwhere customerNo in(select customerNofrom OrderMaster a,OrderDetail b,Product cwhere a.orderNo=b.orderNo and b.productNo=c.productNo andproductName='52倍速光驱') and customerNo in(select customerNofrom OrderMaster a,OrderDetail b,Product cwherea.orderNo=b.orderNo andb.productNo=c.productNo andproductName='17寸显示器')(14) 查找与“陈诗杰”在同一个单位工作的员工姓名、性别、部门和职务。
Selectb.employeeName,b.sex,b.department,b.headS hipfrom Employee a,Employee bwhere a.department=b.department and a.employeeName='陈诗杰'(15) 查询每种商品的商品编号、商品名称、订货数量和订货单价。
SELECTa.productNo,productName,quantity,priceFROM Product a, OrderDetail bWHERE a.productNo=b.productNoORDER BY productName,price(16) 查询单价高于400元的商品编号、商品名称、订货数量和订货单价。
SELECTa.productNo,productName,quantity,priceFROM Product a, OrderDetail bWHERE a.productNo=b.productNo AND price>400ORDER BY productName(17) 分别使用左外连接、右外连接、完整外部连接查询单价高于400元的商品编号、商品名称、订货数量和订货单价,并分析比较检索的结果。
左外连接命令:SELECTa.productNo,productName,quantity,priceFROM Product a LEFT OUTER JOIN OrderDetail bON a.productNo=b.productNo WHERE price>400, b.quantity,b.priceFrom OrderDetail As b left JOIN Product As aON (a.productNo=b.productNo)and price>400Order by a.productNo右外连接命令:SELECTa.productNo,productName,quantity,priceFROM Product a RIGHT OUTER JOIN OrderDetail bON a.productNo=b.productNo WHERE price>400Select a.productNo , a.productName , b.quantity,b.priceFrom OrderDetail As b RIGHT JOIN Product As aON (a.productNo=b.productNo)and price>400Order by a.productNo全连接命令:SELECTa.productNo,productName,quantity,priceFROM Product a FULL OUTER JOINOrderDetail bON a.productNo=b.productNo WHERE price>400Select a.productNo , a.productName , b.quantity,b.priceFrom OrderDetail As b full JOIN Product As aON (a.productNo=b.productNo)and price>400Order by a.productNo(18)分别使用左外连接、右外连接、完整外部连接查询客户编号、客户名称、订货金额和订单编号,并分析比较检索的结果。
SELECTa.customerNo,customerName,orderSum,orderN oFROM Customer a left OUTER JOIN OrderMaster bON a.customerNo=b.customerNoSELECTa.customerNo,customerName,orderSum,orderN oFROM Customer a right OUTER JOIN OrderMaster bON a.customerNo=b.customerNoSELECTa.customerNo,customerName,orderSum,orderN oFROM Customer a full OUTER JOIN OrderMaster bON a.customerNo=b.customerNo从上述结果可知:若表a和表b做外连接,且b表是外码表,则a和b表左外连接可能会出现空值,但是右连接一定不会出现空值,全外连接与左外连接一样的结果。