实验四 数据查询——简单查询

  • 格式:doc
  • 大小:60.50 KB
  • 文档页数:5

实验四数据查询——简单查询一、实验目的1.掌握SQL查询语句的基本概念2.掌握SQLServer查询语句的基本语法3.熟练使用SQL的Select语句对单表进行查询4.熟练掌握并运用SQLServer所提供的函数5.熟练使用SQL语句进行连接操作二、实验环境(实验的软件、硬件环境)硬件:PC机软件:SQL2000三、实验说明请复习相关的单表查询及select语句的语法知识点,并完成如下内容。

四、实验内容1.在订单数据库orderDB中,完成如下的查询:(1)查询所有业务部门的员工姓名,职称,薪水select employeename,headship,salsryfrom employeewhere Department<>' '(2)查询名字中含有“有限”的客户姓名和所在地select customername,adderssfrom customerwhere customername like'%有限%'or customername like'%有限'(3)查询姓“王”并且姓名的最后一个字为“成”的员工select*from employeewhere employeeName like'王%成'(4)查询住址中含有上海或南昌的女员工,并显示其姓名,所属部门,职称,住址,其中性别用“男”和“女”显示。

select employeeName,Department,Headshipfrom employeewhere address='上海市'or Address='南昌市'and Sex='F'(5)查询订单金额高于8000的所有客户编号select customerNofrom orderMasterwhere Ordersum>8000(6)选取编号界于C0001~C0004的客户编号,客户名称,客户地址select customername,Adderss,CustomerNofrom customerwhere CustomerNo between'c2005001'and'c2005004'(7)找出同一天进入公司服务的员工select distincta.employeeName,a.employeeNo,b.employeeName,b.employeeNofrom employee a,employee bwhere a.Hiredate=b.Hiredate and a.employeeNo<>b.employeeNo(8)在订单主表中查询订单金额大于“E2005002”业务员在2008-1-9这天所接的任一张订单的金额”的所有订单信息。

select*from orderMasterwhere Ordersum>any(select Ordersumfrom orderMasterwhere SaleNo='E2005003'andconvert(varchar(20),Orderdate,120)like'2008-01-09%')(9)查询既订购了“52倍速光驱”商品,&&又订购了“17寸显示器”商品的客户编号、订单编号和订单金额。

select customerNo,orderDetail.OrderNo,Ordersumfrom orderDetail,orderMaster,productwhere product.ProductNo=orderDetail.ProductNo andorderDetail.OrderNo=orderMaster.OrderNo and product.ProductNo in(select ProductNofrom productwhere ProductName='52倍速光驱'or ProductName='17寸显示器')select customerNo,OrderNo,Ordersumfrom orderMasterwhere orderno in(select ordernofrom orderDetailwhere ProductNo in(select distinct productnofrom productwhere ProductName='52倍速光驱')) intersectselect customerNo,OrderNo,Ordersumfrom orderMasterwhere orderno in(select ordernofrom orderDetailwhere ProductNo in(select distinct productnofrom productwhere ProductName='17寸显示器')) (10)查找与“陈诗杰”在同一个单位工作的员工姓名、性别、部门和职务。

select employeeName,Sex,Hiredate,Departmentfrom employeewhere Department in(select Departmentfrom employeewhere employeeName='陈诗杰')(11)查询单价高于400元的商品编号、商品名称、订货数量和订货单价select product.ProductNo,ProductName,Qty,Pricefrom product,orderDetailwhere product.ProductNo=orderDetail.ProductNo andproduct.ProductPrice> 400。

(12)分别使用左外连接、右外连接、完整外部连接查询单价高于400元的商品编号、商品名称、订货数量和订货单价,并分析比较检索的结果。

selectproduct.ProductNo,product.ProductName,orderDetail.Qty,orderDetail.Pri cefrom product left join orderDetailon(product.ProductNo=orderDetail.ProductNo)where Price>=40(13)查找每个员工的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期,其中性别使用“男”和“女”表示,日期使用yyyy-mm-dd格式显示。

selecta.employeeNo,a.employeeName,b.Price,b.Qty,d.ProductName,c.Orderdate,c .Ordersum,case sexwhen'F'then'女'else'男'endfrom employee a,orderDetail b,orderMaster c,product dwhere a.employeeNo=c.SaleNo and b.OrderNo=c.OrderNo andb.ProductNo=d.ProductNo(14)查找在2008年3月中有销售记录的客户编号、名称和订单总额。

select orderMaster.customerNo,orderMaster.Ordersum,customernamefrom orderMaster,customerwhere orderMaster.customerNo=customer.CustomerNo(15)使用左外连接查找每个客户的客户编号、名称、订单日期、订货金额,其中订货日期不要显示时间,日期格式为yyyy-mm-dd,按客户编号排序,同一客户再按订单金额降序排序输出。

selectcustomer.CustomerNo,customer.customername,convert(varchar(20),orderMa ster.Orderdate,3),orderMaster.Ordersumfrom customer left join orderMasteron(orderMaster.customerNo=customer.CustomerNo)(16)查找16M DRAM的销售情况,要求显示相应的销售员的姓名,性别,销售日期、销售数量和金额,其中性别用“男”,“女”表示。

selectemployeeName,Sex,orderMaster.Orderdate,orderDetail.Price,orderDetail. Qtyfrom product,employee,orderMaster,orderDetailwhere ProductName='52倍速光驱'andproduct.ProductNo=orderDetail.ProductNo andorderMaster.SaleNo=employee.employeeNo andorderDetail.OrderNo=orderMaster.OrderNo(17)查找公司男业务员所接且订单金额超过2000元的订单号及订单金额。

select orderno,ordersumfrom orderMaster,employeewhere employee.Sex='F'and orderMaster.SaleNo=employee.employeeNo and orderMaster(18)查找来自上海市的客户的姓名,电话,订单号及订单金额。

selectcustomer.customername,customer.Telephone,orderMaster.OrderNo,orderMas ter.Ordersumfrom customer,orderMasterWHERE customer.Adderss='北京市'andorderMaster.customerNo=customer.CustomerNo五、实验步骤请完成实验内容,并写出具体的实验步骤六、思考题:1.连接操作类型有哪些?分析外连接在现实应用中的意义?2.查询表可以用在什么地方?使用查询表要注意哪些地方?3.分析between…and、and、or等关键字的使用方法?4.分析哪些情况需要使用自身连接?5.总结SQL语句中单表查询语句的使用方法?七、总结(实验过程的体会、心得和实验教与学之间还需改进的内容)。