Anda di halaman 1dari 3

CIS 360: SQL Queries

Northwind Data Schema

Northwind Table Names:


Categories
CustomerCustomerDemographics
CustomerDemographics
Customers
Employees
EmployeeTerritories
Order Details
Orders
Products
Shippers
Suppliers
Territories

Transact-SQL Queries
Write a query to fulfill each of the following data requests based upon the Northwind
database (see data schema shown above). Please write clearly.
Data Request 1: Find all SupplierIDs, and ProductIDs only where the Unit Price is greater
than $10.
SELECT S.SupplierID, P.ProductID
FROM Supplier S LEFT JOIN Product P on S.SupplierID = P.SupplierID
WHERE P.UnitPrice > 10;
Data Request 2: Find the category name and Customer contact title for all customers
from Munich, Germany.
SELECT C.CategoryName, CU.ContactTitle
FROM Categories C, Products P, [Order Details] OD, Orders O, Customers CU
WHERE C.CategoryID = P.CategoryID AND P.ProductID = OD.ProductID AND OD.OrderID =
O.OrderID AND O.CustomerID = CU.CustomerID
AND CU.City = Munich AND CU.Country = Germany;
Data Request 3: Find the ID of all employees who have processed orders to be shipped
to Italy, but not those who have Freight costs less than $100. Use a nested query for this.
SELECT E.EmployeeID
FROM Employee E, Order O
WHERE E.EmployeeID = O.EmployeeID
AND O.ShipCountry = Italy
AND O.OrderID NOT IN
(SELECT OrderID
FROM Orders
WHERE Freight < 100);

Data Request 4: Find all the ShipperIDs and the OrderID only of those orders shipped by
UPS.
SELECT S.ShipperID, O.OrderID
FROM Orders O RIGHT JOIN Shippers S ON S.ShipperID = O.ShipVia
WHERE S.CompanyName = UPS;

Please note: The ShipperID=ShipVia link is not the normal way of doing this link, but it
matches what the diagram shows.

Data Request 5: Find the ContactName for all Customers who have paid Freight, but not
where the Quantity ordered was more than 50. Use a nested query for this.
SELECT C.ContactName
FROM Customers C, Orders O
WHERE C.CustomerID = O.CustomerID
AND O.Freight IS NOT NULL
AND O.OrderID NOT IN
(SELECT O.OrderID
FROM Orders O, [Order Details] OD
WHERE O.OrderID = OD.OrderID
AND OD.Quantity > 50);
Data Request 6: Find the product name of all products that are discontinued but still
have units in stock, but only if we get more than 30 products from the supplier. Use a
nested query for this.
SELECT ProductName
FROM Products
WHERE Discontinued = TRUE AND UnitsInStock > 0
AND SupplierID IN
(SELECT S.SupplierID
FROM Products P, Suppliers S
WHERE S.SupplierID = P.SupplierID
HAVING COUNT(P.ProductID) > 30);
Data Request 7: Find all Order IDs, but the EmployeeID of the employee who assisted
with those orders only for employees hired after January 1st 2012.
SELECT O.OrderID, E.EmployeeID
FROM Orders O LEFT JOIN Employees E ON O.EmployeeID=E.EmployeeID
WHERE E.HireDate > 1/1/2012;