数据库系统原理设计实验教程实验2详解.doc
文本预览下载声明
实验二题目
(1) 查找有销售记录的客户编号、名称和订单总额。
select a.customerNo,a.customerName,b.orderNo,sum(quantity*price)
from Customer a,OrderMaster b,OrderDetail c
where a.customerNo=b.customerNo and b.orderNo=c.orderNo
group by a.customerNo,a.customerName,b.orderNo
(2) 在订单明细表中查询订单金额最高的订单。
select top 1 orderNo,sum(quantity*price) orderSum
from OrderDetail
group by orderNo
order by orderSum desc
或:
select orderNo,sum(quantity*price) orderSum
from OrderDetail
group by orderNo
having sum(quantity*price)=
(select max(orderSum)
from (select orderNo,sum(quantity*price) orderSum
from OrderDetail
group by orderNo) b)
(3) 查询没有订购商品的客户编号和客户名称。
SELECT a.customerNo,customerName
FROM Customer a
WHERE a.customerNo NOT IN (SELECT customerNo
FROM OrderMaster )
(3) 找出至少被订购3次的商品编号、订单编号、订货数量和订货金额,并按订货数量的降序排序输出。
SELECT a.productNo,orderNo,quantity,(quantity*price) money
FROM OrderDetail a,
(SELECT productNo
FROM OrderDetail
GROUP BY productNo
HAVING count(*)=3) b
WHERE a.productNo =b.productNo
ORDER BY a.productNo,quantity DESC
(4) 使用子查询查找16M DRAM的销售情况,要求显示相应的销售员的姓名、性别,销售日期、销售数量和金额,其中性别用“男”、“女”表示。
SELECT employeeName,case sex
when F then 女
when M then 男 end sex,
orderDate,quantity,quantity*price 金额
FROM Employee a,OrderMaster b,OrderDetail c
WHERE a.employeeNo=b.salerNo AND b.orderNo=c.orderNo
AND c.ProductNo IN (
SELECT f.ProductNo
FROM OrderMaster d,OrderDetail e,Product f
WHERE d.orderNo=e.orderNo AND ProductName=32M DRAM)
(5) 查询OrderMaster表中订单金额最高的订单号及订单金额。
select orderNo,orderSum
from OrderMaster
where orderSum = (select max(orderSum)
from OrderMaster)
(6) 计算出一共销售了几种商品。
select count(distinct productNo)
from OrderDetail
(7) 显示OrderDetail表中每种商品的订购金额总和,并且依据销售金额由大到小排序输出。
select productNo,sum(quantity*price) summoney
from OrderDetail
group by productNo
order by summoney desc
(8) 查找销售总额大于1000元的销售员编号、姓名和销售额。
select a.employeeNo,a
显示全部