实验四  数据查询——简单查询
一、实验目的
1.掌握SQL查询语句的基本概念
2.掌握SQLServer查询语句的基本语法
3.熟练使用SQLSelect语句对单表进行查询
4.熟练掌握并运用SQLServer所提供的函数
5.熟练使用SQL语句进行连接操作
二、实验环境(实验的软件、硬件环境)
硬件:PC 软件:SQL2000
三、实验说明
    请复习相关的单表查询及select语句的语法知识点,并完成如下内容。
四、实验内容
1.在订单数据库orderDB中,完成如下的查询:
1)查询所有业务部门的员工姓名,职称,薪水
select employeename,headship,salsry
from employee
where Department<>' '
2)查询名字中含有“有限”的客户姓名和所在地
select customername,adderss
from customer
where customername like '%有限%' or customername like '%有限'
3)查询姓“王”并且姓名的最后一个字为“成”的员工
select *
from employee
where employeeName like '%'
4)查询住址中含有上海或南昌的女员工,并显示其姓名,所属部门,职称,住址,其中性别用“男”和“女”显示。
select employeeName,Department,Headship
from employee
where address ='上海市' or Address='南昌市' and Sex='F'
5)查询订单金额高于8000的所有客户编号
select customerNo
from orderMaster
where Ordersum>8000
6)选取编号界于C0001~C0004的客户编号,客户名称,客户地址
select customername,Adderss,CustomerNo
from customer
where CustomerNo between 'c2005001' and 'c2005004'
7)出同一天进入公司服务的员工
select distinct a.employeeName,a.employeeNo,b.employeeName,b.employeeNo
from employee a,employee b
where a.Hiredate=b.Hiredate and a.employeeNo<>b.employeeNo
8)在订单主表中查询订单金额大于“E2005002”业务员在2008-1-9这天所接的任一张订单的金额”的所有订单信息。
select *
from orderMaster
where Ordersum> any (select Ordersum
                    from orderMaster
                    where SaleNo='E2005003' and convert(varchar(20),Orderdate,120) like '2008-01-09%')
9)查询既订购了“52倍速光驱”商品,&&订购了“17寸显示器”商品的客户编号、订单编号和订单金额。
select customerNo,orderDetail.OrderNo,Ordersum
from orderDetail,orderMaster,product
where product.ProductNo=orderDetail.ProductNo and orderDetail.OrderNo=orderMaster.OrderNo and product.ProductNo in(
                    select ProductNo
                    from product
                    where ProductName ='52倍速光驱' or ProductName='17寸显示器'
)
select customerNo,OrderNo,Ordersum
from orderMaster
where orderno in(
                select orderno
                from orderDetail
                where ProductNo in(
                                    select distinct productno
                                    from product
                                    whereselect语句查询日期 ProductName='52倍速光驱'))
intersect
select customerNo,OrderNo,Ordersum
from orderMaster
where orderno in(
                select orderno
                from orderDetail
                where ProductNo in(
                                    select distinct productno
                                    from product
                                    where ProductName='17寸显示器'))
10)查与“陈诗杰”在同一个单位工作的员工姓名、性别、部门和职务。
select employeeName,Sex,Hiredate,Department
from employee
where Department in( select Department
                    from employee
                    where employeeName='陈诗杰'
)
11)查询单价高于400元的商品编号、商品名称、订货数量和订货单价
select product.ProductNo,ProductName,Qty,Price
from product,orderDetail
where product.ProductNo=orderDetail.ProductNo and product.ProductPrice> 400
12)分别使用左外连接、右外连接、完整外部连接查询单价高于400的商品编号、商品名称、订货数量和订货单价,并分析比较检索的结果。
select product.ProductNo,product.ProductName,orderDetail.Qty,orderDetail.Price
from product left join orderDetail on(product.ProductNo=orderDetail.ProductNo)
where Price>=40
13)查每个员工的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期,其中性别使用“男”和“女”表示,日期使用yyyy-mm-dd格式显示。
select a.employeeNo,a.employeeName,b.Price,b.Qty,d.ProductName,c.Orderdate,c.Ordersum,case sex
when 'F' then '' else '' end
from employee a,orderDetail b,orderMaster c,product  d
where a.employeeNo=c.SaleNo and b.OrderNo=c.OrderNo and b.ProductNo=d.ProductNo
14)查在20083月中有销售记录的客户编号、名称和订单总额。
select orderMaster.customerNo,orderMaster.Ordersum,customername
from orderMaster,customer
where orderMaster.customerNo=customer.CustomerNo
15)使用左外连接查每个客户的客户编号、名称、订单日期、订货金额,其中订货日期不要显示时间,日期格式为yyyy-mm-dd,按客户编号排序,同一客户再按订单金额降序排序输出。
select customer.CustomerNo,customer.customername,convert(varchar(20),orderMaster.Orderdate,3),orderMaster.Ordersum
from customer left join orderMaster on(orderMaster.customerNo=customer.CustomerNo)
16)查16M DRAM的销售情况,要求显示相应的销售员的姓名,性别,销售日期、销售数量和金额,其中性别用“男”,“女”表示。
select employeeName,Sex,orderMaster.Orderdate,orderDetail.Price,orderDetail.Qty
from product,employee,orderMaster,orderDetail
where ProductName='52倍速光驱' and product.ProductNo=orderDetail.ProductNo and orderMaster.SaleNo=employee.employeeNo and orderDetail.OrderNo=orderMaster.OrderNo
17)查公司男业务员所接且订单金额超过2000元的订单号及订单金额。
select orderno,ordersum
from orderMaster,employee
where employee.Sex='F' and orderMaster.SaleNo=employee.employeeNo and orderMaster
18)查来自上海市的客户的姓名,电话,订单号及订单金额。
select customer.customername,customer.Telephone,orderMaster.OrderNo,orderMaster.Ordersum
from customer,orderMaster
WHERE customer.Adderss='北京市' and orderMaster.customerNo=customer.CustomerNo