Você está na página 1de 3

BMGT402

Spring 2013 Lab #3 SQL DML


Due: 11:59pm, Monday, March 4, 2013 For this lab, you will be creating queries based on the SQL Server database, AdventureWorks. This database is visible in Management Studio (MS) when you sign on to your student database area. Expand Databases and you will see AdventureWorks at the top of the database list. It is NOT in your space but is a public database, visible to all student users. Your deliverable for this lab will be a document containing your SQL DML statements and the output from executing the statements. For each query assignment below, please cut and paste your SQL code from MS into Microsoft Word and type a statement indicating how many rows were returned. Depending on questions, you may need to copy and paste several rows from the output table. Be sure to name your document yourLastName_yourFirstName_Lab3.docx (or yourLastName_yourFirstName_Lab3.doc). Failing to follow this naming convention will be considered that you fail to satisfy one requirement. When you complete this lab assignment, submit your Word document via Canvas. Using the AdventureWorks database, please perform the following tasks: 1. Using the query design tool in MS, create a query that lists all columns from the Production.ProductListPriceHistory table. How many rows returned?
SELECT * FROM AdventureWorks.Production.ProductListPriceHistory ; -- 395 rows

2. Using the query design tool in MS, create a query that lists all Products from the

Production.Product table where the color is Silver. How many rows returned?
SELECT * FROM AdventureWorks.Production.Product WHERE Color = 'Silver' ; -- 43 rows

3. Using the query design tool in MS, create a query that lists only the VendorID,

AccountNumber, Name, and CreditRating in alphabetical order by Name from the Purchasing.Vendor table. How many rows returned?
SELECT VendorID, AccountNumber, Name, CreditRating FROM AdventureWorks.Purchasing.Vendor ORDER BY Name ; -- 104 rows

4. Using the query design tool in MS, create an Employee Directory by executing a query

that lists employees LastName, FirstName, BirthDate, MaritalStatus, Gender, and HireDate from the HumanResources.Employee and Person.Contact tables. Order the output by LastName. How many rows returned? BMGT402 Lab 3


SELECT c.LastName, c.FirstName, e.BirthDate, e.MaritalStatus, e.Gender, e.HireDate FROM AdventureWorks.Person.Contact c, AdventureWorks.HumanResources.Employee e WHERE c.ContactID = e.ContactID ORDER BY c.LastName ; -- 290 rows

5. Using the query design tool in MS, create a query that lists LastName and FirstName

(from Person.Contact), OrderDate (from Sales.SalesOrderHeader), and ItemValue (created from OrderQty * UnitPrice from Sales.SalesOrderDetail) AND where OrderDate >= July 1, 2004. Order the output by LastName. How many rows returned? (HINT: This is a 3-table query.)
SELECT c.LastName, c.FirstName, s.OrderDate, d.OrderQty*d.UnitPrice AS Item_Value FROM AdventureWorks.Person.Contact c, AdventureWorks.Sales.SalesOrderHeader s, AdventureWorks.Sales.SalesOrderDetail d WHERE c.ContactID = s.ContactID AND s.SalesOrderID = d.SalesOrderID AND s.OrderDate >= '7/01/2004' ORDER BY c.LastName ; -- 2209 rows

6. Using the query design tool in MS, create a query that computes the average of

LineTotal (label the result as Avg_Item_Value) from the Sales.SalesOrderDetail table. What is the Avg_Item_Value returned?
SELECT AVG (LineTotal) AS Avg_Item_Value FROM AdventureWorks.Sales.SalesOrderDetail ; -- Avg_Item_Value = 905.449206

7. Using the query design tool in MS, compute the total (use TotalDue column and label

the SUM as Sum_of_Orders) of all orders from the Sales.SalesOrderHeader table from 12/1/2003 to and including 12/31/2003. What is the Sum_of_Orders returned?
SELECT SUM (TotalDue) AS Sum_of_Orders FROM AdventureWorks.Sales.SalesOrderHeader WHERE OrderDate >= '12/01/2003' AND OrderDate <= '12/31/2003' ; -- Sum_of_Orders = 6582833.0438

8. 'Sheela Word' changed departments multiple times. Using the query design tool in MS,

list the name of the departments 'Sheela Word' worked for, and also list the date 'Sheela Word' started to work for each department. How many departments did 'Sheela Word' work for?
SELECT d.Name, h.StartDate FROM AdventureWorks.HumanResources.EmployeeDepartmentHistory h, AdventureWorks.HumanResources.Employee e, AdventureWorks.HumanResources.Department d, AdventureWorks.Person.Contact c

BMGT402 Lab 3


WHERE h.EmployeeID = e.EmployeeID AND h.DepartmentID = d.DepartmentID AND e.ContactID = c.ContactID AND c.LastName = 'Word' AND c.FirstName = 'Sheela' ; -- 3 rows

9. Using the query design tool in MS, list the total number of sales orders (use

Sales.SalesOrderHeader) that is not paid with a credit card. What is the total number returned?
SELECT COUNT (*) AS Num_of_Orders FROM AdventureWorks.Sales.SalesOrderHeader h WHERE h.CreditCardID IS NULL ; -- Num_of_Orders = 1131

10. Using the query design tool in MS, count the number of addresses (use

Person.Address) that contain 'Santa' as part of any attribute used to represent the addresses. What is the number returned?
SELECT COUNT (*) AS Num_of_Addresses FROM AdventureWorks.Person.Address a WHERE AddressLine1 like '%Santa%' OR AddressLine2 like '%Santa%' OR City like '%Santa%' -- Num_of_Addresses = 292

BMGT402 Lab 3

Você também pode gostar