Sort and Filter Query Results
Sort and Filter Query Results
In this lab, you'll use the Transact-SQL SELECT statement to query and filter data 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.
1. Modify the existing query to return the Name and ListPrice of all products:
2. SELECT Name, ListPrice
FROM SalesLT.Product;
3. Run the query and note that the results are presented in no particular order.
4. Modify the query to add an ORDER BY clause that sorts the results by Name,
as shown here:
5. SELECT Name, ListPrice
6. FROM SalesLT.Product
ORDER BY Name;
7. Run the query and review the results. This time the products are listed in
alphabetical order by Name.
8. Modify the query as shown below to sort the results by ListPrice.
9. SELECT Name, ListPrice
10. FROM SalesLT.Product
ORDER BY ListPrice;
11. Run the query and note that the results are listed in ascending order of ListPrice.
By default, the ORDER BY clause applies an ascending sort order to the
specified field.
12. Modify the query as shown below to sort the results into descending order
of ListPrice.
13. SELECT Name, ListPrice
14. FROM SalesLT.Product
ORDER BY ListPrice DESC;
15. Run the query and note that the results now show the most expensive items first.
16. Modify the query as shown below to sort the results into descending order
of ListPrice, and then into ascending order of Name.
17. SELECT Name, ListPrice
18. FROM SalesLT.Product
ORDER BY ListPrice DESC, Name ASC;
19. Run the query and review the results. Note that they are sorted into descending
order of ListPrice, and each set of products with the same price is sorted in
ascending order of Name.
1. Modify the existing query to return the Name and ListPrice of all products:
2. SELECT TOP (20) Name, ListPrice
3. FROM SalesLT.Product
ORDER BY ListPrice DESC;
4. Run the query and note that the results contain the first twenty products in
descending order of ListPrice. Typically, you include an ORDER BY clause
when using the TOP parameter; otherwise the query just returns the first
specified number of rows in an arbitrary order.
5. Modify the query to add the WITH TIES parameter as shown here, and re-run
it.
6. SELECT TOP (20) WITH TIES Name, ListPrice
7. FROM SalesLT.Product
ORDER BY ListPrice DESC;
8. This time, there are 21 rows in the results, because there are multiple products
that share the same price, one of which wasn't included when ties were ignored
by the previous query.
9. Modify the query to add the PERCENT parameter as shown here, and re-run it.
10. SELECT TOP (20) PERCENT WITH TIES Name, ListPrice
11. FROM SalesLT.Product
ORDER BY ListPrice DESC;
12. Note that this time the results contain the 20% most expensive products.
1. Modify the existing query to return product Name and ListPrice values:
2. SELECT Name, ListPrice
3. FROM SalesLT.Product
ORDER BY Name OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
4. Run the query and note the effect of the OFFSET and FETCH parameters of
the ORDER BY clause. The results start at the 0 position (the beginning of the
result set) and include only the next 10 rows, essentially defining the first page
of results with 10 rows per page.
5. Modify the query as shown here, and run it to retrieve the next page of results.
6. SELECT Name, ListPrice
7. FROM SalesLT.Product
ORDER BY Name OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
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 Color
FROM SalesLT.Product;
5. Use the ⏵Run button to run the query, and and after a few seconds, review the
results, which includes the color of each product in the table.
6. Modify the query as follows, and re-run it.
7. SELECT ALL Color
FROM SalesLT.Product;
The results should be the same as before. The ALL parameter is the default
behavior, and is applied implicitly to return a row for every record that meets the
query criteria.
10. Run the modified query and note that the results include one row for each
unique Color value. This ability to remove duplicates from the results can often
be useful - for example to retrieve values in order to populate a drop-down list of
color options in a user interface.
11. Modify the query to add the Size field as shown here:
12. SELECT DISTINCT Color, Size
FROM SalesLT.Product;
13. Run the modified query and note that it returns each unique combination of
color and size.
1. In Azure Data Studio, replace the existing query with the following code:
2. SELECT Name, Color, Size
3. FROM SalesLT.Product
4. WHERE ProductModelID = 6
ORDER BY Name;
5. Run the query and review the results, which contain the Name, Color,
and Size for each product with a ProductModelID value of 6 (this is the ID for
the HL Road Frame product model, of which there are multiple variants).
6. Replace the query with the following code, which uses the not equal (<>)
operator, and run it.
7. SELECT Name, Color, Size
8. FROM SalesLT.Product
9. WHERE ProductModelID <> 6
ORDER BY Name;
10. Review the results, noting that they contain all products with
a ProductModelID other than 6.
11. Replace the query with the following code, and run it.
12. SELECT Name, ListPrice
13. FROM SalesLT.Product
14. WHERE ListPrice > 1000.00
ORDER BY ListPrice;
15. Review the results, noting that they contain all products with a ListPrice greater
than 1000.00.
16. Modify the query as follows, and run it.
17. SELECT Name, ListPrice
18. FROM SalesLT.Product
WHERE Name LIKE 'HL Road Frame %';
19. Review the results, noting that the LIKE operator enables you to match string
patterns. The % character in the predicate is a wildcard for one or more
characters, so the query returns all rows where the Name is HL Road
Frame followed by any string.
The LIKE operator can be used to define complex pattern matches based on
regular expressions, which can be useful when dealing with string data that
follows a predictable pattern.
23. Review the results. This time the results include products with
a ProductNumber that matches patterns similar to FR-xNNx-NN (in which x is
a letter and N is a numeral).
Tip: To learn more about pattern-matching with the LIKE operator, see
the Transact-SQL documentation.
27. Note that to filter based on NULL values you must use IS NULL (or IS NOT
NULL) you cannot compare a NULL value using the = operator.
28. Now try the following query, which uses the BETWEEN operator to define a
filter based on values within a defined range.
29. SELECT Name
30. FROM SalesLT.Product
WHERE SellEndDate BETWEEN '2006/1/1' AND '2006/12/31';
31. Review the results, which contain products that the company stopped selling in
2006.
32. Run the following query, which retrieves products with
a ProductCategoryID value that is in a specified list.
33. SELECT ProductCategoryID, Name, ListPrice
34. FROM SalesLT.Product
WHERE ProductCategoryID IN (5,6,7);
35. Now try the following query, which uses the AND operator to combine two
criteria.
36. SELECT ProductCategoryID, Name, ListPrice, SellEndDate
37. FROM SalesLT.Product
WHERE ProductCategoryID IN (5,6,7) AND SellEndDate IS NULL;
38. Try the following query, which filters the results to include rows that match one
(or both) of two criteria.
39. SELECT Name, ProductCategoryID, ProductNumber
40. FROM SalesLT.Product
WHERE ProductNumber LIKE 'FR%' OR ProductCategoryID IN (5,6,7);
Challenges
Now that you've seen some examples of filtering and sorting data, it's your chance to
put what you've learned into practice.
Tip: Try to determine the appropriate queries for yourself. If you get stuck, suggested
answers are provided at the end of this lab.
The logistics manager at Adventure Works has asked you to generate some reports
containing details of the company’s customers to help to reduce transportation costs.
The Production Manager at Adventure Works would like you to create some reports
listing details of the products that you sell.
Challenge 1
Challenge 2
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.