You are on page 1of 2

file:///C|/Documents%20and%20Settings/mcrane/Desktop/answers.

txt

--1. Retrieve all orders shipped between 1997 and 1998 (use YEAR() to obtain year from a date attribute). SELECT * FROM Orders WHERE YEAR(ShippedDate) BETWEEN 1997 and 1998 --2. Retrieve all products (ProductID, ProductName) for supplier with ID 7.!!!!!!!!!!!!!!! SELECT ProductID, ProductName FROM Products WHERE SupplierID=7 --3. List all suppliers (CompanyName, City) with company name containg string 'CO'. SELECT CompanyName, City FROM Suppliers WHERE CompanyName like '%Co%' --4. List all distinct products supplied by company 'Pavlova, Ltd.'. SELECT DISTINCT p.ProductName, s.supplierID FROM Products p, Suppliers s WHERE p.SupplierID=s.SupplierID and s.CompanyName='Pavlova, Ltd.' --5. List all employees (FirstName, LastName) handling orders that sell product 'Aniseed Syrup'. SELECT DISTINCT e.FirstName, e.LastName FROM Employees e, Orders o, [Order Details] od, Products p WHERE e.EmployeeID=o.EmployeeID and o.OrderID=od.orderID and od.ProductID=p.ProductID and p.ProductName='Aniseed Syrup' --6. Obtain the number of products supplied by each supplier (SupplierID, ProductCount). Compute total of products at the end. SELECT SupplierID, COUNT(ProductID) as ProductCount FROM Products GROUP BY SupplierID COMPUTE SUM(COUNT(ProductID)) --7. Obtain total quantity on order for each product (ProductName, UnitsOnOrder). Compute total at the end. SELECT p.ProductName, SUM(od.Quantity) as Quantity FROM Products p, [Order Details] od WHERE p.productID=od.ProductID GROUP BY p.ProductName COMPUTE SUM(SUM(od.Quantity)) --8. Compute total values of sales for each employee in 1997(OrderDate) (FirstName, LastName, Sale). Order results descending. SELECT e.FirstName, e.LastName, SUM(od.Quantity*od.UnitPrice-od.Discount) as Sale FROM Employees e, [Order Details] od, Orders o WHERE e.EmployeeID=o.EmployeeID and o.OrderID=od.OrderID and year(o.OrderDate)=1997 GROUP BY e.FirstName, e.LastName ORDER BY SUM(od.Quantity*od.UnitPrice-od.Discount) DESC --9. Retrieve all products that have been ordered in a quantity larger than 100 in 1997, in descending order (ProductName, Quantity).

file:///C|/Documents%20and%20Settings/mcrane/Desktop/answers.txt (1 of 2) [01/04/2010 11:31:36]

file:///C|/Documents%20and%20Settings/mcrane/Desktop/answers.txt

SELECT p.ProductName, SUM(od.Quantity) as Quantity FROM Products p, [Order Details] od, Orders o WHERE p.ProductID=od.ProductID and od.OrderID = o.OrderID and YEAR(o.OrderDate)=1997 GROUP BY p.ProductName HAVING SUM(od.Quantity)>500 ORDER BY SUM(od.Quantity) DESC

--10.Retrieve top 5 selling products (ProductName, TotalQuantity) in category 'Beverages', in descending order. SELECT TOP 5 p.ProductName, SUM(o.Quantity) as Quantity FROM Products p, [Order Details] o, Categories c WHERE p.ProductID=o.ProductID and p.CategoryID=c.CategoryID and c.CategoryName='Beverages' GROUP BY p.ProductID, p.ProductName ORDER BY SUM(o.Quantity) DESC

file:///C|/Documents%20and%20Settings/mcrane/Desktop/answers.txt (2 of 2) [01/04/2010 11:31:36]

You might also like