教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 行业资料 >

Northwind数据库练习及参考答案

来源:网络收集 时间:2026-07-08
导读: Northwind数据库练习及参考答案 Northwind数据库练习及参考答案 单表查询 --查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期、订单I D、客户ID和雇员ID等字段的值 Create View Orderquery as Select OrderDate,OrderID,CustomerID,EmployeeI

Northwind数据库练习及参考答案

Northwind数据库练习及参考答案

单表查询

--查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期、订单I

D、客户ID和雇员ID等字段的值

Create View Orderquery

as

Select OrderDate,OrderID,CustomerID,EmployeeID

from Orders

where OrderDate Between '1996-07-01' and '1996-07-15'

Select * from Orderquery

--查询“Northwind”示例数据库中供应商的ID、公司名称、地区、城市和电话字段的值。条件是“地区等于华北”并且“联系人头衔等于销售代表”。

Select SupplierID,CompanyName,Address,City

from suppliers

where Region='华北' and ContactTitle='销售代表'

--查询“Northwind”示例数据库中供应商的ID、公司名称、地区、城市和电话字段的值。其中的一些供应商位于华东或华南地区,另外一些供应商所在的城市是天津

Select SupplierID,CompanyName,Region,City,Phone

from Suppliers

where Region in ('东北','华南')

or City='天津'

--查询“Northwind”示例数据库中位于“华东”或“华南”地区的供应商的ID、公司名称、地区、城市和电话字段的值

Select SupplierID,CompanyName,Region,City,Phone

from Suppliers

where Region in ('东北','华南')

多表查询

--查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期、订单I

D、相应订单的客户公司名称、负责订单的雇员的姓氏和名字等字段的值,并将查询结果按雇员的“姓氏”和“名字”字段的升序排列,“姓氏”和“名字”值相同的记录按“订单 ID”的降序排列

Create procedure orderquery2

@StartOrderDate datetime='1998-01-02 00:00:00.000',

@EndOrderDate datetime='1998-01-31 23:59:59.997'

with encryption

as

Select Orders.OrderDate,Orders.OrderID,http://www.77cn.com.cnpanyName,http://www.77cn.com.cnstName,Employees.FirstName

from Orders join Customers

Northwind数据库练习及参考答案

on Customers.CustomerID=Orders.CustomerID

join Employees

on Employees.EmployeeID=Orders.EmployeeID

Where OrderDate between @StartOrderDate and @EndOrderDate or OrderDate between '1998-01-01 00:00:00.000' and '1998-01-31 23:59:59.997' Order By LastName,FirstName ASC,OrderID DESC

execute orderquery2 '1996-07-01 00:00:00.000','1996-07-15 23:59:59.999'

--查询“10248”和“10254”号订单的订单ID、运货商的公司名称、订单上所订购的产品的名称

Create view orderquery3

as

Select Orders.OrderID,http://www.77cn.com.cnpanyName,ProductName

From Orders join Shippers

on Shippers.ShipperID=Orders.ShipVia

join [Order Details]

on [Order Details].OrderID=Orders.OrderID

join Products

on Products.ProductID=[Order Details].ProductID

Select * from orderquery3

where OrderID =10248 or OrderID=10254

--查询“10248”和“10254”号订单的订单ID、订单上所订购的产品的名称、数量、单价和折扣

Create view orderquery4

as

Select Orders.OrderID,ProductName,quantity,Products.unitprice

From [Order Details] join Orders

on [Order Details].orderid=Orders.orderid

join Products

on Products.ProductID=[Order Details].ProductID

Select * from orderquery3

where OrderID =10248 or OrderID=10254

--查询“10248”和“10254”号订单的订单ID、订单上所订购的产品的名称及其销售金额 Create view orderquery5

as

Select Orders.OrderID,ProductName,Products.unitprice*quantity as '销售金额' From [Order Details] join Orders

on [Order Details].orderid=Orders.orderid

join Products

on Products.ProductID=[Order Details].ProductID

Select * from orderquery5

where OrderID =10248 or OrderID=10254

Northwind数据库练习及参考答案

综合查询

--查询所有运货商的公司名称和电话

select companyname,phone

from Shippers

--查询所有客户的公司名称、电话、传真、地址、联系人姓名和联系人头衔

select companyname,fax,phone,address,contactname,contacttitle

from customers

--查询单价介于10至30元的所有产品的产品ID、产品名称和库存量

select productid,productname,unitsinstock

from products

where unitprice between 10 and 30

--查询单价大于20元的所有产品的产品名称、单价以及供应商的公司名称、电话 select productname,unitprice,http://www.77cn.com.cnpanyname,suppliers.phone from suppliers join products

on suppliers.supplierid=products.supplierid

where unitprice>20

--查询上海和北京的客户在1996年订购的所有订单的订单ID、所订购的产品名称和数量 select orders.orderid,productname,quantity,city

from [order details] join products

on [order details].productid=products.productid

join orders

on [order details].orderid=orders.orderid

join customers

on orders.customerid=customers.customerid

where city in('北京' ,'上海')

and

OrderDate between '1996-00-00 00:00:00' and '1996-12-31 23:59:59.999'

--查询华北客户的每份订单的订单ID、产品名称和销售金额

select orders.orderid,productname,[order details].unitprice*quantity as 销售金额

from [order details] join products

on [order details].productid=products.productid

join orders

on [order details].orderid=orders.orderid

join customers

on orders.customerid=customers.customerid

where region='华北'

--按运货商公司名称,统计1997年由各个运货商承运的订单的总数量

select companyname,count(*)

from shippers join orders

on shippers.shipperid=orders.shipvia

Northwind数据库练习及参考答案

where year(orderdate)=1997

group by companyname

--统计1997年上半年的每份订单上所订购的产品的总数量 select orders.orderid,sum(quantity)

from [order details] join orders

on [order details].orderid=orders.orderid

where year(orderdate)=1997 and month(orderdate)>=1 and month(orderdate)<=6

group by orders.orderid

--select * from [order details] join orders

on [order details].orderid=orders.orderid

where orders.orderid=10400 and year(orderdate)=1997 --统计各类产品的平均价格

select categories.category …… 此处隐藏:2732字,全部文档内容请下载后查看。喜欢就下载吧 ……

Northwind数据库练习及参考答案.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/2275169.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)