一、单表查询
--1.查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期、订单ID、客户ID和雇员ID等字段的值 SELECT OrderID ,
CustomerID ,
EmployeeID ,
OrderDate FROM dbo.Orders WHERE OrderDate BETWEEN '' AND ''
--2.--查询“Northwind”示例数据库中供应商的ID、公司名称、地区、城市和电话字段的值。条件是“地区等于Western”并且“联系人头衔等于Sales Representative”。 SELECT SupplierID ,
CompanyName ,
City ,
Region ,
Phone FROM [dbo].Suppliers WHERE Region='Western' AND ContactTitle='Sales Representative'
二、多表查询
--6.查询“10248”和“10254”号订单的订单ID、运货商的公司名称、订单上所订购的产品的名称 SELECT a.OrderID AS '订单ID',a.ShipName AS '运货商的公司',d.ProductName AS '产品的名称' FROM [dbo].[Orders] AS a
JOIN dbo.Shippers AS b ON b.ShipperID = a.ShipVia
JOIN dbo.[Order Details] AS c ON c.OrderID = a.OrderID JOIN dbo.Products AS d ON d.ProductID = c.ProductID
WHERE a.OrderID=10248 OR a.OrderID=10254;
--10.查询单价介于10至30元的所有产品的产品ID、产品名称和库存量
SELECT ProductID AS '产品ID',
ProductName AS '产品名称',
UnitsInStock AS '库存量' FROM [dbo].[Products] WHERE UnitPrice BETWEEN 10 AND 30
--11.--查询 单价大于20元 的所有 产品 的 ‘产品名称’、‘单价’以及‘供应商的公司名称’、‘电话’ SELECT a.ProductName AS '产品名称',
a.UnitPrice AS '单价',
b.CompanyName AS '供应商的公司名称',
b.Phone AS '电话'
FROM [dbo].[Products] AS a
JOIN dbo.Suppliers AS b ON b.SupplierID = a.SupplierID;
--7.查询“10248”和“10254”号 '订单' 的订单ID、订单上所订购的'产品'的名称及其销售金额
SELECT a.OrderID AS '订单ID',c.ProductName AS '产品名',a.UnitPrice*a.Quantity AS '销售金额' FROM [dbo].[Order Details] AS a
JOIN dbo.Orders AS b ON b.OrderID = a.OrderID
JOIN dbo.Products AS c ON c.ProductID = a.ProductID WHERE a.OrderID=10248 OR a.OrderID=10254;
三、综合查询
--8.查询所有运货商的公司名称和电话
select a.CompanyName ,
a.Phone from [dbo].[Shippers] as a
--11.--查询 单价大于20元 的所有 产品 的 ‘产品名称’、‘单价’以及‘供应商的公司名称’、‘电话’ SELECT a.ProductName AS '产品名称',
a.UnitPrice AS '单价',
b.CompanyName AS '供应商的公司名称',
b.Phone AS '电话'
FROM [dbo].[Products] AS a
JOIN dbo.Suppliers AS b ON b.SupplierID = a.SupplierID
WHERE a.UnitPrice>20;
--12.--查询 London和Sao Paulo的客户([dbo].[Customers]) 在1996年 订购的所有订单的'订单ID'、所订购的'产品名称'和'数量' SELECT b.OrderID,c.ProductName,b.Quantity FROM
dbo.Orders AS a
JOIN dbo.[Order Details] AS b ON b.OrderID = a.OrderID
JOIN dbo.Products AS c ON c.ProductID = b.ProductID
JOIN dbo.Customers AS d ON d.CustomerID = a.CustomerID
WHERE (d.City='London' OR d.City='Sao Paulo') AND DATEPART(YEAR,a.OrderDate)='';
--13.查询地区为NM 客户 的每份 订单 的 '订单ID'、'产品名称'和'销售金额'
SELECT b.OrderID,d.ProductName,c.UnitPrice*c.Quantity AS '销售金额'
FROM dbo.Customers AS a
JOIN dbo.Orders AS b ON b.CustomerID = a.CustomerID
JOIN dbo.[Order Details] AS c ON c.OrderID = b.OrderID
JOIN dbo.Products AS d ON d.ProductID = c.ProductID
WHERE a.Region = 'NM'
--14.按 运货商公司名称,统计 1997年 由各个运货商承运的 '订单的总数量' SELECT a.CompanyName AS '运货商公司名称',COUNT(*) AS '由运货商承运的订单的总数量'
FROM [dbo].[Shippers] AS a
JOIN dbo.Orders AS b ON b.ShipVia = a.ShipperID WHERE YEAR(b.OrderDate)=''
GROUP BY a.CompanyName
--15.统计 1997年上半年 的 每份订单 上所订购的 产品 的 总'数量'
SELECT a.OrderID AS '产品',SUM(a.Quantity) AS '总数量' FROM [dbo].[Order Details] AS a
JOIN dbo.Orders AS b ON b.OrderID = a.OrderID
WHERE b.OrderDate>'' GROUP BY a.OrderID
--16.统计 各类产品 的 平均价格
SELECT b.CategoryName,AVG(a.UnitPrice) AS '平均价格' FROM [dbo].[Products] AS a
JOIN dbo.Categories AS b ON b.CategoryID = a.CategoryID
GROUP BY b.CategoryName