实验报告1:数据库查询_第1页
实验报告1:数据库查询_第2页
实验报告1:数据库查询_第3页
实验报告1:数据库查询_第4页
实验报告1:数据库查询_第5页
已阅读5页,还剩26页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第1个实验单表查询 第2章实验一(1)查询员工的姓名、职务和薪水。select employeeName,headShip,salaryfrom Employee(2) 查询名字中含有“有限”的客户名称和所在地。select customerName,address from Customerwhere customerName like'%有限%'(3)查询出姓“张”并且姓名的最后一个字为“梅”的员工。select *from Employeewhere employeeName like'%张_梅%'(4)查询住址中含有“上海”或“南昌”的女员工,并显示其姓

2、名、所属部门、职称、住址、出生日期和性别,其中如果出生日期为空,显示“不详”,否则按格式“yyyy-mm-dd”显示,性别用“男”和“女”显示。select employeeName,headShip,address,department, sex=CASE sex WHEN 'M' THEN '男' WHEN 'F' THEN '女' ELSE '不确定' END, isnull(convert( char(10),birthday,120),'不详')出生日期from Employeewhere

3、 sex='F' and (address like '%上海%'or address like '%南昌%' ) (5)查询出职务为“职员”或职务为“科长”的女员工的信息。select *from Employeewhere sex='F' and (headShip like '职员'or headShip like '科长' )(6)选取编号不在C20050001C20050004之间的客户编号、客户名称、客户地址。select customerNo,customerName, address

4、 from Customer WherecustomerNo not in('C20050001','C20050002','C20050003','C20050004')(7) 在表OrderMaster中挑出销售金额大于等于5000元的订单。select * from OrderMasterwhere orderSum>=5000(8)选取订单金额最高的前10%的订单数据。select top 10 * from OrderMasterorder by orderSum DESC(9)计算出一共销售了几种商品。selec

5、t count(distinct productNo )from OrderDetail(10)计算OrderDetail表中每种商品的销售数量、平均销售单价和总销售金额,并且依据销售金额由大到小排序输出。select productNo 产品编号,sum(quantity) 销售数量,sum(price*quantity)总销售金额,sum(price*quantity)/sum(quantity) 平均销售单价from OrderDetailgroup by productNoorder by sum(price*quantity) desc(11)按客户编号统计每个客户2008年2月的订

6、单总金额。select customerNo 客户编号, sum(orderSum)订单金额from OrderMasterwhere year(orderDate)=2008 and month(orderDate)=2group by customerNo (12)统计至少销售了10件以上的商品编号和销售数量。select productNo 产品编号,sum(quantity) 销售数量from OrderDetailgroup by productNohaving sum(quantity)>=10(13)统计在业务科工作且在1973年或1967年出生的员工人数和平均工资。sel

7、ect count(*)员工人数,sum(salary)/count(*) 平均工资from Employeewhere department='财务科'and (year(birthday)=1973 or year(birthday)>=1967)(14) 实验问题: 给出SQL语句实现分组聚集操作的执行过程。在分组聚合中,先对查询结果进行分组,然后聚合函数分别作用于每个组,查询结果按组聚合输出。也就是先执行from 语句;再执行where语句;其次执行group by语句;之后使用聚合函数进行计算;最后使用having语句进行筛选分组 WHERE和HAVING子句都

8、是用于指定查询条件的,请给出你对这两个子句的理解,用实例说明。Where子句和having子句的作用对象不同,where子句作用于表和视图,having子句作用于组group,Having子句必须和group by子句配合使用;Where在分组和聚集计算之前选取输入行,因此,它控制那些行进行聚集计算,而having在分组和聚集之后选取分组的行。因此where子句不包含聚集函数,相反,having子句总是包含聚集函数的。实例:Where子句,查询住址在南昌的员工信息select *from Employee Where address like '%南昌%'Having子句,查询

9、业绩最好的业务员的名字 select a.employeeName from Employee a,( select top 1 sum(orderSum) 金额,salerNo from OrderMaster group by salerNo ) b where a.employeeNo=b.salerNo 在分组聚集操作中,为什么在查询列中,除了集聚函数运算,其它表达式必须包含在GROUP BY子句中。如果查询列除聚集函数外的表达式不包含在group by子句中,则聚合没有意义。 分析条件BETWEEN . AND、AND、OR等关键字的使用方法。BETWEEN . AND是范围查询,可

10、用于查询属性值在某一范围内的元组,between后是属性的下限值,and后是属性的上限值;AND是逻辑查询,实现逻辑与运算;OR是逻辑查询,实现逻辑或运算。 请总结SQL语句中的单表查询语句的使用方法。SQL语句中的简单查询语句有:1. 查询所有列Select * from 表名2. 查询指定列Select 列名 from 表名3. 消除重复元组Select distinct 列名 from 表名4. 给属性列取别名Select 列名 as 别名From 表名5. 字符匹配查询Like <匹配字符串> escape 换码字符6. 排序运算Order by <表达式1>

11、asc|desc ,<表达式2> asc|desc第2个实验多表查询 第2章实验二(1)找出同一天进入公司服务的员工。select distinct a.employeeNo,a.hireDatefrom Employee a,Employee bwhere a.hireDate=b.hireDate and a.employeeNo!=b.employeeNo (2)查找与“陈诗杰”在同一个单位工作的员工姓名、性别、部门和职务。select employeeName, sex, department,headShipfrom Employee where department i

12、n (select department from Employee where employeeName='陈诗杰') and employeeNo not in (select employeeNo from Employee where employeeName='陈诗杰') (3)在Employee表中查询薪水超过员工平均薪水的员工信息。select *from Employee where salary>=(select sum(salary)/count(*) from Employee ) (4) 查找有销售记录的客户编号、名称和订单总额。s

13、elect a.customerNo,customerName ,orderSum from Customer a,OrderMaster bwhere a.customerNo=b.customerNo and a.customerNo in(select customerNo from OrderMaster)(5)查询没有订购商品的客户编号和客户名称。select customerNo,customerName from Customer where customerNo not in(select customerNo from OrderMaster)(6)使用子查询查找32M DR

14、AM的销售情况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用“男”、“女”表示。select e.employeeName, sex=CASE sex WHEN 'M' THEN '男' WHEN 'F' THEN '女' ELSE '不确定' END, f.orderDate,f.orderSum ,d.quantityfrom Employee e,OrderMaster f,OrderDetail dwhere f.salerNo=e.employeeNo and d.orderNo

15、=f.orderNo and ductNo in(select productNo from Product where productName='32M DRAM' )(7)查询OrderMaster表中订单金额最高的订单号及订单金额。select orderSum,orderNo from OrderMaster where orderSum=(select max(orderSum)from OrderMaster )(8)在订单主表中查询订单金额大于“E2005002业务员在2008-1-9这天所接的任一张订单的金额”的所有订单信息。select *from

16、OrderMasterwhere orderSum >(select min(orderSum)from OrderMaster where orderDate='20080109' and salerNo='E2005002')(9)查询单价高于400元的商品编号、商品名称、订货数量和订货单价。select a.orderNo,ductName,a.quantity,a.price from OrderDetail a,Product bwhere ductNo=ductNo and a.price>400(10)分别

17、使用左外连接、右外连接、完整外部连接查询单价高于400元的商品编号、商品名称、订货数量和订货单价,并分析比较检索的结果。左外连接select a.orderNo,ductName,a.quantity,a.price from OrderDetail a left outer join Product b on ductNo=ductNo where a.price>400右外连接select a.orderNo,ductName,a.quantity,a.price from OrderDetail a right outer join Prod

18、uct b on ductNo=ductNo where a.price>400完整外部连接select a.orderNo,ductName,a.quantity,a.price from OrderDetail a full outer join Product b on ductNo=ductNo where a.price>400select a.orderNo,ductName,a.quantity,a.price from OrderDetail a full outer join Product b on a

19、.productNo=ductNo where a.price>400(11)使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额,其中订货日期不要显示时间,日期格式为“yyyy-mm-dd”,按客户编号排序,同一客户再按订单金额降序排序输出。select a.customerNo ,a.customerName ,isnull(convert( char(10),b.orderDate,120),'不详'),b.orderSum from Customer a right outer join OrderMaster b on a.customerNo

20、=b.customerNo order by a.customerNo , b.orderSum desc(12)查找每个员工的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期,其中性别使用“男”和“女”表示,日期使用“yyyy-mm-dd”格式显示。Select a.employeeNo ,a.employeeName ,a.sex,ductName,c.quantity,c.price,b.orderSum,isnull(convert( char(10),b.orderDate,120),'不详')日期from Employee

21、a, OrderMaster b,OrderDetail c,Product dwhere a.employeeNo=b.salerNo and b.orderNo=c.orderNo and ductNo=ductNo(13) 查找32M DRAM的销售情况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用“男”、“女”表示。select e.employeeName, sex=CASE sex WHEN 'M' THEN '男' WHEN 'F' THEN '女' ELSE '

22、不确定' END, f.orderDate,f.orderSum ,d.quantityfrom Employee e,OrderMaster f,OrderDetail dwhere f.salerNo=e.employeeNo and d.orderNo=f.orderNo and ductNo in(select productNo from Product where productName='32M DRAM' )(14)找出公司男业务员所接且订单金额超过2000元的订单号及订单金额。 select orderNo, orderSum from Or

23、derMaster where salerNo in(select employeeNo from Employee where sex='M' )and orderSum>2000(15)求出每位客户的总订购金额,显示出客户号及总订购金额,并按总订购金额降序排列。Select customerNo, sum(orderSum )总订购金额from OrderMaster group by customerNoorder by sum(orderSum ) desc(16)查询每种商品的总销售数量及总销售金额,要求显示出商品编号、商品名称、总数量及总金额,并按商品号从小到

24、大排列。select *from (select productNo,productName from Product)a,(select productNo ,sum(quantity)数量, sum(quantity*price)金额from OrderDetail group by productNo)bwhere ductNo=ductNoorder by ductNo(17) 实验问题: 连接操作类型有哪些?分析外连接在现实应用中的意义。连接操作类型包括内连接等值与非等值连接、自表连接、外连接(左外连接,右外连接,全外连接)。外连接的意义:对于存在多对多

25、关系的两个关系,可以使关联不上的部分信息也能显示出来。 查询表可以用在什么地方?使用查询表要注意哪些地方?查询表用在from 子句之后,必须为查询表设置一个变量名,用该名表示查询表。查询表可以作为被查询的表, 分析SQL语句中的IN和OR关键字有何异同点?它们可以互换吗?给出实例说明。In可用于集合查询,也可用于嵌套子查询中;Or则为逻辑查询,逻辑或;当in用于集合查询时,in和or之间可以相互转换;当in用于嵌套子查询时,则不可以相互转换。举例:选取编号在C20050001C20050004之间的客户编号、客户名称、客户地址。使用in查询:select customerNo,customer

26、Name, address from CustomerWhere customerNo in('C20050001','C20050002','C20050003','C20050004')使用or查询:select customerNo,customerName, address from Customer Where customerNo='C20050001'or customerNo='C20050002'or customerNo='C20050003'or custome

27、rNo='C20050004'当in用于子查询时,in和or不可以互换举例:查询单笔订单金额查过2000的客户名select customerName from Customer Where customerNo in(select customerNo from OrderMaster where orderSum >2000) 分析哪几种情况需要使用自表连接。当同一表中的某一元组变量与另外的元组变量相关时,可以使用自表连接,从而得到一些特殊的数据。自表连接一般用来判断并筛选单一表中的一些数据。 第3个实验复杂查询 第2章实验三(1) 在订单明细表中查询订单金额最高的订

28、单。 select *from OrderDetailwhere quantity*price=(select max(quantity*price) from OrderDetail)(2) 找出至少被订购3次的商品编号、订单编号、订货数量和订货金额,并按订货数量的降序排序输出。select ductNo,a.orderNo,(quantity* quantity)订货金额from OrderDetail awhere ductNo in(select productNo from OrderDetail group by productNo having count(

29、productNo)>=3 )(3) 查找销售总额少于5000元的销售员编号、姓名和销售额。select a.employeeNo ,a.employeeName ,sum(b.orderSum )业绩额from Employee a, OrderMaster bwhere a.employeeNo=b.salerNo group by a.employeeNo ,a.employeeName having sum(b.orderSum )>=5000 、 (4) 找出目前业绩未超过5000元的员工,并按销售业绩的降序排序输出。select a.employeeNo ,a.empl

30、oyeeName ,sum(b.orderSum )业绩额from Employee a, OrderMaster bwhere a.employeeNo=b.salerNo group by a.employeeNo ,a.employeeName having sum(b.orderSum )<5000 order by sum(b.orderSum ) desc(5) 查询订单中所订购的商品数量没有超过10个的客户编号和客户名称。. 在某客户所有订单中,只要有一次订单中订购的商品总数量超过10时,就认为不符合题目要求select c.customerNo,c.customerNam

31、e from Customer c,OrderMaster awhere a.customerNo=c.customerNo and a.orderNo not in (select orderNo from OrderDetail group by orderNo having sum(quantity)>10).在某客户所有订单中,当全部订单中每次订购的商品总数量都超过10 时,就认为不符合题目要求select c.customerNo,c.customerName from Customer c,OrderMaster awhere a.customerNo=c.customerN

32、o and a.orderNo in (select orderNo from OrderDetail group by orderNo having sum(quantity)<=10).在某客户所有订单中,当全部订单累计的总订购商品数量超过10 时,就认为不符合题目要求select c.customerNo,c.customerName from Customer cwhere c.customerNo in (select b.customerNo from OrderDetail a, OrderMaster b where a.orderNo=b.orderNo group b

33、y b.orderNo,b.customerNo having sum(quantity)<=10)(6) 查找订货金额最大的客户名称和总货款。select a.customerName, b.总金额from Customer a, ( select top 1 sum(orderSum) 总金额, customerNo from OrderMaster group by customerNo ) bwhere a.customerNo=b.customerNo (7) 查找至少订购了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额。select ductNo,d.

34、productName ,e.customerNo, e.customerName ,f.price , f.quantity from Product d, Customer e, OrderDetail f, OrderMaster g,(select customerNo from OrderMaster a, OrderDetail b where a.orderNo=b.orderNo group by customerNo having count(productNo)>=3) ductNo=ductNo andf.orderNo=g.orde

35、rNo andg. customerNo=e. customerNo ande.customerNo=c.customerNo(8) 找出目前销售业绩超过4000元的业务员编号及销售业绩,并按销售业绩从大到小排序。select salerNo , sum(orderSum )销售业绩from OrderMastergroup by salerNo having sum(orderSum )>4000(9) 求每位客户订购的每种商品的总数量及平均单价,并按客户号、商品号从小到大排列。select a.customerNo ,ductNo,sum(quantity)总数量,sum(

36、price)/sum(quantity)平均单价from OrderMaster a, OrderDetail bwhere a.orderNo=b.orderNo group by a.customerNo,ductNo order by a.customerNo ,ductNo(10) 查询业绩最好的的业务员号、业务员名及其总销售金额。select employeeNo , employeeName ,销售业绩from Employee a,(select top 1 sum(orderSum )销售业绩,salerNo from OrderMaster group b

37、y salerNo )bwhere a.employeeNo =b.salerNo(11) 查询订购的商品至少包含了订单“200803010001”中所订购商品的订单。select orderNofrom OrderDetail xwhere not exists( select * from OrderDetail y where orderNo='200803010001' and not exists ( select * from OrderDetail where productNo=ductNo and orderNo=x.orderNo )(12)查询总

38、订购金额超过“C20070002”客户的总订购金额的客户号、客户名及其住址。select a.customerNo , customerName, address from Customer a,( select sum(orderSum )订购金额, customerNo from OrderMaster group by customerNo )bwhere a.customerNo=b. customerNo and 订购金额>( select sum(orderSum ) from OrderMaster group by customerNo having customerNo

39、='C20070002')(13)查询总销售金额最高的销售员编号、订单编号、订单日期和订单金额。select orderNo ,orderSum,b.salerNo , orderDatefrom OrderMaster a,(select top 1 sum(orderSum )销售业绩,salerNo from OrderMaster group by salerNo )bwhere a.salerNo=b.salerNo (14) 用存在量词查找没有订货记录的客户名称。select customerName from Customer xwhere not exists

40、( select * from OrderMaster a where x.customerNo=a.customerNo)(15)查询既订购了“52倍速光驱”商品,又订购了“17寸显示器”商品的客户编号、订单编号和订单金额。select customerNo,orderNo,orderSumfrom OrderMaster where customerNo in( select a.customerNo from OrderMaster a, OrderMaster b,OrderDetail c, OrderDetail d where a.customerNo=b.customerNo and c.orderNo=a.orderNo and d.orderNo=b.orderNo and ductNo=(select productNo from Product where productName='52倍速光驱' ) and ductNo=(select productNo from Product where productName='17寸显示器

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论