数据库
首页 > 数据库> > 数据库实验4

数据库实验4

作者:互联网

select employeename,headship,salary from Employee
/查询所有员工的姓名,职务,薪水/

select customername,Address from customer where CustomerName like’%有限%’
/查询名字中含有限的客户名和地址/

select * from Employee where EmployeeName like’王%成’
/查询姓王且名字最后一个字为成的员工/

select employeename,department,headship,address,Sex=case sex when ‘M’ then ‘男’ else ‘女’ end from Employee
where (Address=’%上海%’ or Address=’%南昌%’) and Sex=‘F’
/查询住址中含上海或南昌的女员工的姓名,部门,职称,住址,性别,性别用男女表示/

select customerno from orderMaster
where Ordersum>8000
/查询订单金额高于8000的所有客户编号/

select customername,address from customer
where CustomerNo between ‘c2005001’ and ‘c2005004’
/选取编号介于c2005001~c2005004的客户编号,客户名称,客户地址/

select * from Employee a
where exists
(select Hiredate,EmployeeNo from Employee b
where a.Hiredate=b.Hiredate and a.EmployeeNo!=b.EmployeeNo)
/找出同一天进入公司服务的员工/

select * from orderMaster
where Ordersum>any(select Ordersum from orderMaster
where Orderdate=‘2008-1-9’ and SaleNo=‘E2005002’)
/在订单主表中查询订单金额大于指定业务员某一天最大订单金额的所有订单信息/

select customerNo,OrderNo,Ordersum from orderMaster a
where exists
(select a.OrderNo,c.orderNo from orderMaster a,orderDetail c
where a.OrderNo=c.OrderNo and exists
(select b.ProductNo,c.ProductNo from orderDetail c,product b
where c.ProductNo=b.ProductNo and b.ProductName=‘52倍速光驱’ or b.ProductName=‘17寸显示器’ and exists(
select CustomerNo,OrderNo from orderMaster d
where a.CustomerNo=d.CustomerNo and a.OrderNo!=d.OrderNo)))
/查询既订购了“52倍速光驱”又订购了“17寸显示器”的客户编号、订单编号和订单金额/

select customerno,orderno,ordersum from orderMaster a
where exists
(select b.customerno,c.customerno from
(select f.CustomerNo from orderDetail e,orderMaster f,product g
where e.ProductNo=g.ProductNo and g.ProductName=‘52倍速光驱’ and e.OrderNo=f.OrderNo)b,
(select y.CustomerNo from orderDetail x,orderMaster y,product z
where z.ProductNo=x.ProductNo and z.ProductName=‘17寸显示器’ and x.OrderNo=y.OrderNo)c
where b.CustomerNo=c.CustomerNo)order by CustomerNo

select employeename,sex,department,headship from Employee a
where exists
(select a.Department,b.department,b.EmployeeName from Employee b
where a.Department=b.Department and b.EmployeeName=‘陈诗杰’)
/查询和陈诗杰在同一个部门的员工姓名、性别、部门、职务/

select a.ProductNo,a.ProductName,sum(b.Qty),b.OrderPrice from product a,orderDetail b
where b.OrderPrice>400 and a.ProductNo=b.ProductNo
group by a.ProductNo,b.OrderPrice,a.ProductName
/查询订货单价高于400元的商品编号、商品名称、订货数量和订货单价/

select a.productno,a.productname,SUM(b.qty),b.orderprice from product a left outer join orderDetail b
on a.ProductNo=b.ProductNo and b.OrderPrice>400
group by a.ProductName,a.ProductNo,b.OrderPrice
/使用左外连接查询订货单价高于400元的商品编号、商品名称、订货数量和订货单价/

select a.productno,a.productname,SUM(b.qty),b.orderprice from product a right outer join orderDetail b
on a.ProductNo=b.ProductNo and b.OrderPrice>400
group by a.ProductName,a.ProductNo,b.OrderPrice
/使用右外连接查询订货单价高于400元的商品编号、商品名称、订货数量和订货单价/

select a.productno,a.productname,SUM(b.qty),b.orderprice from product a full outer join orderDetail b
on a.ProductNo=b.ProductNo and b.OrderPrice>400
group by a.ProductNo,a.ProductName,b.OrderPrice
/使用完整外部连接查询订货单价高于400元的商品编号、商品名称、订货数量和订货单价/

select a.SaleNo,b.EmployeeName,d.ProductName,SUM(c.Qty),c.OrderPrice,c.qty*c.orderprice,Orderdate=CONVERT(varchar(10),Orderdate,120),Sex=case sex when ‘F’ then ‘女’ else ‘男’ end from orderMaster a,Employee b,orderDetail c,product d
where a.OrderNo=c.OrderNo and a.SaleNo=b.EmployeeNo and c.ProductNo=d.ProductNo
group by a.SaleNo,b.EmployeeName,d.ProductName,c.OrderPrice,a.Ordersum,a.Orderdate,Sex,c.Qty
/查找每个员工的销售记录,要求显示销售员的编号、姓名、性别、商品名称、数量、单价、金额和销售日期,其中性别使用男女表示,日期使用yyyy-mm-dd格式显示/

select a.CustomerNo,b.CustomerName,a.Ordersum from orderMaster a,customer b
where YEAR(a.Orderdate)=2008 and MONTH(a.Orderdate)=3 and a.CustomerNo=b.CustomerNo
/查找在2008年3月有销售记录的的客户编号、名称和订单总额/

select a.CustomerNo,CustomerName,Orderdate=CONVERT(varchar(10),Orderdate,120),Ordersum from customer a left outer join orderMaster b
on a.CustomerNo=b.CustomerNo
order by a.CustomerNo,Ordersum DESC
/使用左外连接查找每个客户的客户编号、名称、订单日期、订单金额,其中订货日期不要显示时间,日期格式为yyyy-mm-dd,按客户编号排序,同一客户再按订单金额降序排序输出/

select employeename,sex=case sex when ‘F’ then ‘女’ else ‘男’ end,orderdate,SUM(qty),qty*orderprice from product a,orderMaster b,orderDetail c,Employee d
where ProductName=‘32M DRAM’ and d.EmployeeNo=b.SaleNo and a.ProductNo=c.ProductNo and b.OrderNo=c.OrderNo
group by d.EmployeeName,d.Sex,b.Orderdate,c.Qty,c.OrderPrice
/查找32M DRAM的销售情况,要求显示相应的销售员的姓名、性别、销售日期、销售数量和金额,其中性别用男女表示/

select orderno,ordersum from orderMaster a,Employee b
where a.SaleNo=b.EmployeeNo and b.Sex=‘M’ and a.Ordersum>2000
/查找公司男业务员所接且订单金额超过2000元的订单号及订单金额/

select customername,telephone,orderno,ordersum from customer a,orderMaster b
where a.CustomerNo=b.CustomerNo and a.Address=‘上海市’
/查找来自上海市的客户的姓名、电话、订单号及订单金额/

标签:ProductNo,CustomerNo,数据库,orderMaster,实验,OrderNo,where,select
来源: https://blog.csdn.net/sajdhasjdd/article/details/110915318