Instructor Inputs Session 9 ¤NIIT Instructor Inputs 9.3 This session includes exercises of Chapter 5 and Chapter 6. Exercise 1 Insert the following data in the ProductBrand table of the AdventureWorks database. BrandID BrandName B01 Lee B02 Nike B03 Reebok Solution You need to execute the following insert statements to create the ProductBrand table and store three records in the ProductBrand table. Create Table ProductBrand ( BrandID varchar(4), BrandName varchar(20) ) INSERT INTO ProductBrand VALUES (‘B01’, ‘Lee’) INSERT INTO ProductBrand VALUES (‘B02’, ‘Nike’) INSERT INTO ProductBrand VALUES (‘B03’, ‘Reebok’) Exercise 2 AdventureWorks, Inc. has set up a new store. Insert the following data into the database: Store Name – Car Store Sales Person ID – 283 Demographics - <StoreSurvey XMLns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/StoreSurvey"> Solutions to Exercises Chapter 5 9.4 Instructor Inputs ¤NIIT Tip <AnnualSales>350000</AnnualSales> <AnnualRevenue>35000</AnnualRevenue> <BankName>International Bank</BankName> <BusinessType>BM</BusinessType> <YearOpened>1980</YearOpened> <Specialty>Road</Specialty> <SquareFeet>7500</SquareFeet> <Brands>AW</Brands> <Internet>T1</Internet> <NumberEmployees>7</NumberEmployees> </StoreSurvey> A store in AdventureWorks is treated like a customer. Therefore, you need to first create a record in the customer table by storing the territory id and specifying the customer type as 'S'. Then you need to add the store details in the Store table. Solution Generate the customerId for the new store by storing the details in the Customer table as follows: INSERT INTO Sales.Customer VALUES (7,'S', DEFAULT, DEFAULT) Store the details of the new store in the Store table as follows: INSERT INTO Sales.Store VALUES(29484, 'Car store', 285, '<StoreSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/StoreSurvey"> <AnnualSales>350000</AnnualSales> <AnnualRevenue>35000</AnnualRevenue> <BankName>International Bank</BankName> <BusinessType>BM</BusinessType> <YearOpened>1980</YearOpened> <Specialty>Road</Specialty> <SquareFeet>7500</SquareFeet> <Brands>AW</Brands> <Internet>T1</Internet> <NumberEmployees>7</NumberEmployees> </StoreSurvey>', DEFAULT, DEFAULT) ¤NIIT Instructor Inputs 9.5 Exercise 3 The address of a vendor, Comfort Road Bicycles, has changed. You need to update the following data in the AdventureWorks database. Address 4151 Olivera City Atlanta StateProvinceID 17 PostalCode 30308 Solution To update the data, write the following statements in the Query Editor window of the Microsoft SQL Server Management Studio window: Update Purchasing.VendorAddress set AddressID = (Select AddressID from Person.Address Where AddressLine1 = '4151 Olivera' AND City = 'Atlanta') FROM Purchasing.VendorAddress va, Purchasing.Vendor v WHERE va.VendorID = v.VendorID AND v.Name = 'Comfort Road Bicycles' Exercise 4 Delete all the records from the ProductBrand table. Ensure that you do not delete the table. Solution To delete all the records, write the following statements in the Query Editor window of the Microsoft SQL Server Management Studio window: Truncate table ProductBrand 9.6 Instructor Inputs ¤NIIT Exercise 5 The users of AdventureWorks need to publish the details of all the customers and their address on the organizations website. To perform this task, you need to retrieve the data in the XML format. Solution To retrieve the data in XML format, write the following statements in the Query Editor window of the Microsoft SQL Server Management Studio window: SELECT c.CustomerID, TerritoryID, AccountNumber, CustomerType, AddressLine1, City, StateProvinceID, PostalCode FROM Sales.Customer c JOIN Sales.CustomerAddress ca ON c.CustomerID = ca.CustomerID JOIN Person.Address a ON ca.AddressID = a.AddressID FOR XML PATH('Customer') Exercise 6 The management of AdventureWorks require a list displaying the skills of all the candidates who have applied for a vacancy. The details of all the candidates are stored in the XML format in the HumanResources.JobCandidate table. Solution To generate the list, write the following statement in the Query Editor window of the Microsoft SQL Server Management Studio window: SELECT JobCandidateID, Resume.value('declare namespace ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/Resume"; (/ns:Resume/ns:Name/ns:Name.First)[1]','nvarchar(20)') AS [First Name], Resume.value('declare namespace ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/Resume"; (/ns:Resume/ns:Name/ns:Name.Last)[1]','nvarchar(20)') AS [Last Name], Resume.value('declare namespace ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/Resume"; ¤NIIT Instructor Inputs 9.7 (/ns:Resume/ns:Skills)[1]','nvarchar(max)') AS [Skills] FROM HumanResources.JobCandidate Exercise 7 The production of a bicycle at AdventureWorks involves a number of phases. In each phase, the bicycle is moved to a different work center. The details of all the work centers are stored in the Production.ProductModel table. Bicycles of different types go through different set of work centres, depending on the components that need to be attached. The management wants a list of all the types of bicycles that go through work center 10. How will you generate this list? Solution To generate the list, write the following statement in the Query Editor window of the Microsoft SQL Server Management Studio window: WITH XMLNAMESPACES (' http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/ProductModelManuInstructions' AS pd ) SELECT ProductModelID FROM Production.ProductModel WHERE Instructions.exist('/pd:root/pd:Location[@LocationID=10]' ) = 1 Exercise 8 There is a change in the production process of the bicycle with the product model id 7. Due to this change, the bicycle will not be going to work centre 10. You need to update this change in the database. How will you perform this task? Solution To update the changes, write the following statements in the Query Editor window of the Microsoft SQL Server Management Studio window: WITH XMLNAMESPACES (' http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/ProductModelManuInstructions' AS pd ) UPDATE Production.ProductModel SET Instructions.modify('delete (/pd:root/pd:Location)[1]' ) WHERE ProductModelID = 7 9.8 Instructor Inputs ¤NIIT Exercise 1 The SalesOrderDetail and SalesOrderHeader tables store the details of the sales orders. To generate a report displaying the sales order id and the total amount of all the products purchased against an order, you are using the following query: SELECT sd.SalesOrderID, sum(LineTotal) AS [Total Amount] FROM Sales.SalesOrderDetail sd JOIN Sales.SalesOrderHeader sh ON sd.SalesOrderID = sh.SalesOrderID GROUP BY sd.SalesOrderID The table contains a large amount of data. Create an appropriate index to optimize the execution of this query. Solution To optimize the query performance, you need to create the clustered index on the SalesOrderID attribute of the table SalesOrderHeader table as this column contains unique and non-null values. To solve the problem, you need to execute the following statement: CREATE NONCLUSTERED INDEX idxSalesOrderID ON Sales.SalesOrderDetail (SalesOrderID) Exercise 2 The Store table is often queried. The queries are based on the CustomerID attribute and take long time to execute. Optimize the execution of the queries. In addition, ensure that the CustomerID attribute does not contain duplicate values. Solution To optimize the execution of the queries, based on the CustomerID attribute of the Store table, you need to create a unique nonclustered index on the CustomerID column. To solve the problem, you need to execute the following statement: CREATE UNIQUE NONCLUSTERED INDEX Idx_CustomerID ON Sales.Store(CustomerID) Chapter 6 ¤NIIT Instructor Inputs 9.9 Exercise 3 The SalesOrderDetail table is often queried. The queries are based on the SalesOrderDetailID and SalesOrderID attributes. The execution of the queries takes a long time. Optimize the execution of the queries. Solution You need to create the nonclustered index on the SalesOrderDetailID and SalesOrderID attributes of the SalesOrderDetail table in order to speed up the query execution. To solve the problem, you need to execute the following statement: CREATE NONCLUSTERED INDEX Idx_SalesOrderId_SalesOrderDetailId ON Sales.SalesOrderDetail(SalesOrderId, SalesOrderDetailId) 9.10 Instructor Inputs ¤NIIT . Instructor Inputs Session 9 ¤NIIT Instructor Inputs 9. 3 This session includes exercises of Chapter 5 and Chapter 6. Exercise. Demographics - <StoreSurvey XMLns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/StoreSurvey"> Solutions to Exercises Chapter 5 9. 4 Instructor Inputs ¤NIIT Tip. namespace ns="http://schemas.microsoft.com/sqlserver/2004/07/adventure- works/Resume"; ¤NIIT Instructor Inputs 9. 7 (/ns:Resume/ns:Skills)[1]','nvarchar(max)') AS [Skills]