Lab5 SQL Exercises

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Exercise 1

The AdventureWorksLT database includes a table-valued user-defined function named


dbo.ufnGetCustomerInformation. Use this function to retrieve details of customers based on customer
ID values retrieved from tables in the database. Retrieve the sales order ID, customer ID, first name, last
name, and total due for all sales orders from the SalesLT.SalesOrderHeader table and the
dbo.ufnGetCustomerInformation function. Make sure to use the aliases provided, and default column
names elsewhere.

Complete the following query:


-- select SalesOrderID, CustomerID, FirstName, LastName, TotalDue from the appropriate
tables
SELECT ___, ___, ___, ___, ___
FROM SalesLT.SalesOrderHeader AS SOH
-- cross apply as per the instructions
___ ___ dbo.ufnGetCustomerInformation(___.___) AS CI
-- finish the clause
ORDER ___ SOH.SalesOrderID;

Exercise 2
AdventureWorks sells many products that are variants of the same product model. You must write
queries that retrieve information about these products. Retrieve the product ID, product name, product
model name, and product model summary for each product from the SalesLT.Product table and the
SalesLT.vProductModelCatalogDescription view. Make sure to use the aliases provided, and default
column names elsewhere.

Complete the following query:


-- select the appropriate columns from the appropriate tables
SELECT ___, ___ AS ProductName, ___.Name AS ProductModel, ___.Summary
FROM SalesLT.Product AS P
___ SalesLT.vProductModelCatalogDescription AS PM
-- join based on ProductModelID
ON ___ = ___
ORDER BY ProductID;

Exercise 3
You are only interested in products which have a listed color in the database. Create a table variable and
populate it with a list of distinct colors from the SalesLT.Product table. Then use the table variable to
filter a query that returns the product ID, name, and color from the SalesLT.Product table so that only
products with a color listed in the table variable are returned. You'll need to use NVARCHAR(15) in your
solution and make sure to use the aliases provided.
Complete the following query:
DECLARE @Colors AS ___ (___ NVARCHAR(15));

INSERT INTO @___


SELECT DISTINCT ___ FROM SalesLT.___;

SELECT ProductID, Name, Color


FROM SalesLT.Product
WHERE ___ IN (SELECT ___ FROM @___);

Exercise 4
The AdventureWorksLT database includes a table-valued function named dbo.ufnGetAllCategories,
which returns a table of product categories (e.g. 'Road Bikes') and parent categories (for example
'Bikes').

The result of dbo.ufnGetAllCategories looks like:

Write a query that uses this function to return a list of all products including their parent category and
their own category. Make sure to use the aliases provided, and default column names elsewhere.

Complete the following query:


SELECT C.___ AS ParentCategory,
C.___ AS Category,
P.ProductID, P.___ AS ProductName
FROM SalesLT.Product AS P
JOIN dbo.___() AS C
ON P.___ = C.___
ORDER BY ParentCategory, Category, ProductName;

Exercise 5
Each AdventureWorks product is stored in the SalesLT.Product table, and each product has a unique
ProductID identifier, which is implemented as an IDENTITY column in the SalesLT.Product table.

Products are organized into categories, which are defined in the SalesLT.ProductCategory table.

The products and product category records are related by a common ProductCategoryID identifier,
which is an IDENTITY column in the SalesLT.ProductCategory table.
The new product to be inserted is shown in this table:

AdventureWorks has started selling the new product shown in the table above. Insert it into the
SalesLT.Product table, using default or NULL values for unspecified columns.

Once you've inserted the product, run SELECT SCOPE_IDENTITY(); to get the last identity value that was
inserted.

Add a query to view the row for the product in the SalesLT.Product table.

Complete the following query:


-- Finish the INSERT statement
INSERT INTO SalesLT.Product (___, ___, ___, ___, ___, ___)
VALUES
(___, ___, ___, ___, ___, ___);

-- Get last identity value that was inserted


SELECT ___();

-- Finish the SELECT statement


___ * FROM ___
WHERE ProductID = SCOPE_IDENTITY();

You might also like