1. 程式人生 > 資料庫 >資料庫實驗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=‘上海市’
/查詢來自上海市的客戶的姓名、電話、訂單號及訂單金額/