0% found this document useful (0 votes)
15 views5 pages

Use Subqueries

fghfg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Use Subqueries

fghfg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Use Subqueries

In this lab, you'll use subqueries to retrieve data from tables in


the adventureworks database. For your reference, the following diagram shows the
tables in the database (you may need to resize the pane to see them clearly).

Note: If you're familiar with the standard AdventureWorks sample database, you may
notice that in this lab we are using a simplified version that makes it easier to focus on
learning Transact-SQL syntax.

Use simple subqueries


A subquery is a query that is nested within another query. The subquery is often
referred to as the inner query, and the query within which it is nested is referred to as
the outer query.

1. Start Azure Data Studio, and create a new query (you can do this from
the File menu or on the welcome page).
2. In the new SQLQuery_… pane, use the Connect button to connect the query to
the AdventureWorks saved connection.
3. In the query editor, enter the following code:
4. SELECT MAX(UnitPrice)
FROM SalesLT.SalesOrderDetail;

5. Use the ⏵Run button to run the query, and and after a few seconds, review the
results, which consists of the maximum UnitPrice in
the SalesLT.SalesOrderDetail (the highest price for which any individual
product has been sold).
6. Modify the query as follows to use the query you just ran as a subquery in an
outer query that retrieves products with a ListPrice higher than the maximum
selling price.
7. SELECT Name, ListPrice
8. FROM SalesLT.Product
9. WHERE ListPrice >
10. (SELECT MAX(UnitPrice)
FROM SalesLT.SalesOrderDetail);

11. Run the query and review the results, which include all products that have
a listPrice that is higher than the maximum price for which any product has
been sold.
12. Replace the existing query with the following code:
13. SELECT DISTINCT ProductID
14. FROM SalesLT.SalesOrderDetail
WHERE OrderQty >= 20;

15. Run the query and note that it returns the ProductID for each product that has
been ordered in quantities of 20 or more.
16. Modify the query as follows to use it in a subquery that finds the names of the
products that have been ordered in quantities of 20 or more.
17. SELECT Name FROM SalesLT.Product
18. WHERE ProductID IN
19. (SELECT DISTINCT ProductID
20. FROM SalesLT.SalesOrderDetail
WHERE OrderQty >= 20);

21. Run the query and note that it returns the product names.
22. Replace the query with the following code:
23. SELECT DISTINCT Name
24. FROM SalesLT.Product AS p
25. JOIN SalesLT.SalesOrderDetail AS o
26. ON p.ProductID = o.ProductID
WHERE OrderQty >= 20;

27. Run the query and note that it returns the same results. Often you can achieve
the same outcome with a subquery or a join, and often a subquery approach can
be more easily interpreted by a developer looking at the code than a complex
join query because the operation can be broken down into discrete components.
In most cases, the performance of equivalent join or subquery operations is
similar, but in some cases where existence checks need to be performed, joins
perform better.

Use correlated subqueries


So far, the subqueries we've used have been independent of the outer query. In some
cases, you might need to use an inner subquery that references a value in the outer
query. Conceptually, the inner query runs once for each row returned by the outer query
(which is why correlated subqueries are sometimes referred to as repeating subqueries).

1. Replace the existing query with the following code:


2. SELECT od.SalesOrderID, od.ProductID, od.OrderQty
3. FROM SalesLT.SalesOrderDetail AS od
ORDER BY od.ProductID;

4. Run the query and note that the results contain the order ID, product ID, and
quantity for each sale of a product.
5. Modify the query as follows to filter it using a subquery in the WHERE clause
that retrieves the maximum purchased quantity for each product retrieved by the
outer query. Note that the inner query references a table alias that is declared in
the outer query.
6. SELECT od.SalesOrderID, od.ProductID, od.OrderQty
7. FROM SalesLT.SalesOrderDetail AS od
8. WHERE od.OrderQty =
9. (SELECT MAX(OrderQty)
10. FROM SalesLT.SalesOrderDetail AS d
11. WHERE od.ProductID = d.ProductID)
ORDER BY od.ProductID;

12. Run the query and review the results, which should only contain product order
records for which the quantity ordered is the maximum ordered for that product.
13. Replace the query with the following code:
14. SELECT o.SalesOrderID, o.OrderDate, o.CustomerID
15. FROM SalesLT.SalesOrderHeader AS o
ORDER BY o.SalesOrderID;

16. Run the query and note that it returns the order ID, order date, and customer ID
for each order that has been placed.
17. Modify the query as follows to retrieve the company name for each customer
using a correlated subquery in the SELECT clause.
18. SELECT o.SalesOrderID, o.OrderDate,
19. (SELECT CompanyName
20. FROM SalesLT.Customer AS c
21. WHERE c.CustomerID = o.CustomerID) AS CompanyName
22. FROM SalesLT.SalesOrderHeader AS o
ORDER BY o.SalesOrderID;

23. Run the query, and verify that the company name is returned for each customer
found by the outer query.

Challenges
Now it's your opportunity to try using subqueries to retrieve data.

Tip: Try to determine the appropriate queries for yourself. If you get stuck, suggested
answers are provided at the end of this lab.
Challenge 1: Retrieve product price information

Adventure Works products each have a standard cost price that indicates the cost of
manufacturing the product, and a list price that indicates the recommended selling price
for the product. This data is stored in the SalesLT.Product table. Whenever a product is
ordered, the actual unit price at which it was sold is also recorded in
the SalesLT.SalesOrderDetail table. You must use subqueries to compare the cost and
list prices for each product with the unit prices charged in each sale.

1. Retrieve products whose list price is higher than the average unit price.
o Retrieve the product ID, name, and list price for each product where the
list price is higher than the average unit price for all products that have
been sold.
o Tip: Use the AVG function to retrieve an average value.
2. Retrieve Products with a list price of 100 or more that have been sold for less
than 100.
o Retrieve the product ID, name, and list price for each product where the
list price is 100 or more, and the product has been sold for less than 100.

Challenge 2: Analyze profitability

The standard cost of a product and the unit price at which it is sold determine its
profitability. You must use correlated subqueries to compare the cost and average
selling price for each product.

1. Retrieve the cost, list price, and average selling price for each product
o Retrieve the product ID, name, cost, and list price for each product along
with the average unit price for which that product has been sold.
2. Retrieve products that have an average selling price that is lower than the cost.
o Filter your previous query to include only products where the cost price
is higher than the average selling price.

Challenge Solutions
This section contains suggested solutions for the challenge queries.

Challenge 1

1. Retrieve products whose list price is higher than the average unit price:
2. SELECT ProductID, Name, ListPrice
3. FROM SalesLT.Product
4. WHERE ListPrice >
5. (SELECT AVG(UnitPrice)
6. FROM SalesLT.SalesOrderDetail)
ORDER BY ProductID;

7. Retrieve Products with a list price of 100 or more that have been sold for less
than 100:
8. SELECT ProductID, Name, ListPrice
9. FROM SalesLT.Product
10. WHERE ProductID IN
11. (SELECT ProductID
12. FROM SalesLT.SalesOrderDetail
13. WHERE UnitPrice < 100.00)
14. AND ListPrice >= 100.00
ORDER BY ProductID;

Challenge 2

1. Retrieve the cost, list price, and average selling price for each product:
2. SELECT p.ProductID, p.Name, p.StandardCost, p.ListPrice,
3. (SELECT AVG(o.UnitPrice)
4. FROM SalesLT.SalesOrderDetail AS o
5. WHERE p.ProductID = o.ProductID) AS AvgSellingPrice
6. FROM SalesLT.Product AS p
ORDER BY p.ProductID;

7. Retrieve products that have an average selling price that is lower than the cost:
8. SELECT p.ProductID, p.Name, p.StandardCost, p.ListPrice,
9. (SELECT AVG(o.UnitPrice)
10. FROM SalesLT.SalesOrderDetail AS o
11. WHERE p.ProductID = o.ProductID) AS AvgSellingPrice
12. FROM SalesLT.Product AS p
13. WHERE StandardCost >
14. (SELECT AVG(od.UnitPrice)
15. FROM SalesLT.SalesOrderDetail AS od
16. WHERE p.ProductID = od.ProductID)
ORDER BY p.ProductID;

Return to Microsoft Learn

1. When you've finished the exercise, complete the knowledge check in Microsoft
Learn.
2. When the link above opens in another browser tab, return to this one to end the lab
environment.

You might also like