学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
实验四 数据查询——简单查询
一、实验目的
1.掌握SQL查询语句的基本概念 2.掌握SQLServer查询语句的基本语法 3.熟练使用SQL的Select语句对单表进行查询 4.熟练掌握并运用SQLServer所提供的函数 5.熟练使用SQL语句进行连接操作 二、实验环境(实验的软件、硬件环境) 硬件:PC机 软件:SQL2000
三、实验说明
请复习相关的单表查询及select语句的语法知识点,并完成如下内容。
四、实验内容
1
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
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 '王%成'
2
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
(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'
3
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
(7)找出同一天进入公司服务的员工
select
a.employeeName,a.employeeNo,b.employeeName,b.employeeNo
distinct
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寸显示器”商品的
4
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
客户编号、订单编号和订单金额。
select customerNo,orderDetail.OrderNo,Ordersum
from orderDetail,orderMaster,product
where product.ProductNo=orderDetail.ProductNo 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
5
and
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
from orderDetail
where ProductNo in(
select distinct productno
from product
where ProductName='52倍速光驱'))
intersect
select customerNo,OrderNo,Ordersum
from orderMaster
where orderno in(
select orderno
from orderDetail
where ProductNo in(
select distinct productno
6
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
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
7
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
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
8
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
where a.employeeNo=c.SaleNo and b.OrderNo=c.OrderNo and
b.ProductNo=d.ProductNo
(14)查找在2008年3月中有销售记录的客户编号、名称和订单总额。
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的销售情况,要求显示相应的销售员的姓名,性别,销售日期、销售数量和金额,其中性别用“男”,“女”表示。
9
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
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
10
数据库原理实验
学号:123012012095 姓名:吴艺萍 班级(3)小班 日期:2014年 月 日
from customer,orderMaster
WHERE customer.Adderss='北京市' and
orderMaster.customerNo=customer.CustomerNo
五、实验步骤
请完成实验内容,并写出具体的实验步骤 六、思考题:
1.连接操作类型有哪些?分析外连接在现实应用中的意义? 2.查询表可以用在什么地方?使用查询表要注意哪些地方? 3.分析between…and、and、or等关键字的使用方法? 4.分析哪些情况需要使用自身连接?
5.总结SQL语句中单表查询语句的使用方法?
七、总结(实验过程的体会、心得和实验教与学之间还需改进的内容)
11
因篇幅问题不能全部显示,请点此查看更多更全内容