Download presentation
Presentation is loading. Please wait.
1
Sorting and Filtering Data
20761B 5: Sorting and Filtering Data Module 5 All demonstrations in this module use a Microsoft Azure® SQL Database, running a copy of the AdventureWorksLT database. Before attempting to run these demos, ensure you have a copy of AdventureWorksLT running on an Azure instance. For detailed steps on creating a copy of the AdventureWorksLT database in Azure see: D:\Creating an AdventureWorks Database on Azure.docx. Start the MSL-TMG1, 20761B-MIA-DC, and 20761B-MIA-SQL virtual machines. Sorting and Filtering Data
2
Working with Unknown Values
20761B Module Overview 5: Sorting and Filtering Data Working with Unknown Values
3
Demonstration: Sorting Data
20761B Lesson 1: Sorting Data 5: Sorting and Filtering Data Demonstration: Sorting Data Question If you declare an alias for a column in the SELECT clause, you cannot use that alias in the WHERE clause—but you can use it in the ORDER BY clause. Why is this? Answer This is because of the order in which clauses of a SELECT query are processed. The WHERE clause is processed before the SELECT column list; therefore, column aliases are not available for sorting. The ORDER BY clause is processed last, so column aliases are available and can be used without errors.
4
Using the ORDER BY Clause
5: Sorting and Filtering Data ORDER BY sorts rows in results for presentation purposes No guaranteed order of rows without ORDER BY Use of ORDER BY guarantees the sort order of the result Last clause to be logically processed Sorts all NULLs together ORDER BY can refer to: Columns by name, alias or ordinal position (not recommended) Columns not part of SELECT list Unless DISTINCT specified Declare sort order with ASC or DESC ORDER BY determines the order of the data and does not affect the set of data selected. Remind students that, without an ORDER BY, the lab exercises may return results in a different order from the supplied lab answers. If they want to check results, they can add an ORDER BY clause, both to their solution and the provided one. This will affect all labs! ORDER BY sorts all NULLs together. See the next lesson for more information on NULL. Use of ordinal position (for example, ORDER by 1,2) is not recommended as, if the SELECT clause changes the position of columns, the ORDER BY clause is not automatically updated to refer to new positions. This may lead to errors in client applications. ASC is the default sort. Note: ORDER BY may also include a COLLATE clause, which helps sort by a specific collation, not the collation of the column in the table. Collations will be discussed further in Module 6 of this course.
5
Writing ORDER BY using column names:
ORDER BY Clause Syntax 5: Sorting and Filtering Data Writing ORDER BY using column names: Writing ORDER BY using column aliases: Specifying sort order in the ORDER BY clause: SELECT <select list> FROM <table source> ORDER BY <column1_name>, <column2_name>; SELECT <column> AS <alias> FROM <table source> ORDER BY <alias1>, <alias2>; SELECT <column> AS <alias> FROM <table source> ORDER BY <column_name|alias> ASC|DESC;
6
ORDER BY Clause Examples
5: Sorting and Filtering Data ORDER BY with column names: ORDER BY with column alias: ORDER BY with descending order: SELECT orderid, custid, orderdate FROM Sales.Orders ORDER BY orderdate; SELECT orderid, custid, YEAR(orderdate) AS orderyear FROM Sales.Orders ORDER BY orderyear; SELECT orderid, custid, orderdate FROM Sales.Orders ORDER BY orderdate DESC;
7
Filtering Data in the WHERE Clause with Predicates
20761B Filtering Data in the WHERE Clause with Predicates 5: Sorting and Filtering Data WHERE clauses use predicates Must be expressed as logical conditions Only rows for which predicate evaluates to TRUE are accepted Values of FALSE or UNKNOWN filtered out WHERE clause follows FROM, precedes other clauses Can’t see aliases declared in SELECT clause Can be optimized by SQL Server to use indexes Data filtered server-side Can reduce network traffic and client memory usage
8
Filter rows for customers from Spain
20761B WHERE Clause Syntax 5: Sorting and Filtering Data Filter rows for customers from Spain Filter rows for orders after July 1, 2007 Filter orders within a range of dates SELECT contactname, country FROM Sales.Customers WHERE country = N'Spain'; SELECT orderid, orderdate FROM Sales.Orders WHERE orderdate > ' '; SELECT orderid, custid, orderdate FROM Sales.Orders WHERE orderdate >= ' ' AND orderdate < ' ';
9
Filtering in the SELECT Clause Using the TOP Option
20761B Filtering in the SELECT Clause Using the TOP Option 5: Sorting and Filtering Data TOP allows you to limit the number or percentage of rows returned by a query Works with ORDER BY clause to limit rows by sort order: If ORDER BY list is not unique, results are not deterministic (no single correct result set) Modify ORDER BY list to ensure uniqueness, or use TOP WITH TIES Added to SELECT clause: SELECT TOP (N) | TOP (N) Percent With percent, number of rows rounded up (nondeterministic) SELECT TOP (N) WITH TIES Retrieve duplicates where applicable (deterministic) TOP is proprietary to Microsoft SQL Server Note that, if TOP is used, ORDER BY fills two purposes—first, to support the TOP operator in the SELECT clause, then again to determine the output order of rows for display purposes. If ORDER BY list is not unique, the results are not deterministic, which means that there is not just one correct set of rows. SQL Server will return rows as they are accessed. To address this, either add expressions to the ORDER BY list to ensure uniqueness, or use TOP (N) with TIES.
10
Filtering in the ORDER BY Clause Using OFFSET-FETCH
5: Sorting and Filtering Data OFFSET-FETCH is an extension to the ORDER BY clause: Allows filtering a requested range of rows Dependent on ORDER BY clause Provides a mechanism for paging through results Specify number of rows to skip, number of rows to retrieve: Available in SQL Server 2012, 2014, and 2016 Provides more compatibility than TOP While TOP may be used without ORDER BY (with unpredictable results), OFFSET/FETCH, without an ORDER BY, will return an error. Note: The code example is only a partial representation of the OFFSET/FETCH syntax. More complete syntax and examples are presented on the next slide. ORDER BY <order_by_list> OFFSET <offset_value> ROW(S) FETCH FIRST|NEXT <fetch_value> ROW(S) ONLY
11
OFFSET-FETCH Syntax OFFSET value must be supplied
5: Sorting and Filtering Data OFFSET value must be supplied May be zero if no skipping is required The optional FETCH clause allows all rows following the OFFSET value to be returned Natural Language approach to code: ROW and ROWS interchangeable FIRST and NEXT interchangeable ONLY optional—makes meaning clearer to human reader OFFSET value and FETCH value may be constants or expressions, including variables and parameters Note that the interchangeability of ROW for ROWs and FIRST for NEXT allows for English language-like code. In an application that calls these queries in sequence, each query is independent and not related to the other. No state is maintained on the server (unlike a cursor). In paging scenarios like the ones mentioned above, it is a best practice to use unique ordering, avoiding the chance of the same row appearing in multiple pages. Unlike those found in any previous modules, examples of OFFSET/FETCH must be executed by SQL Server 2012, 2014 or OFFSET/FETCH was not supported in SQL Server 2008 R2 or earlier. OFFSET <offset_value> ROW|ROWS FETCH FIRST|NEXT <fetch_value> ROW|ROWS [ONLY]
12
SQL Server uses NULLs to mark missing values
20761B Three-Valued Logic 5: Sorting and Filtering Data SQL Server uses NULLs to mark missing values NULL can be "missing but applicable" or "missing but inapplicable" Customer middle name: Not supplied, or doesn’t have one? With no missing values, predicate outputs are TRUE or FALSE only ( 5 > 2, 1=1) With missing values, outputs can be TRUE, FALSE or UNKNOWN (NULL > 99, NULL = NULL) Predicates return UNKNOWN when comparing missing value to another value, including another missing value Missing but applicable: License plate number of an employee who owns an automobile. Missing but inapplicable: License plate number of an employee who is a pedestrian. Celko’s joke: Hair color of a bald man? (He’s bald.) Predicates should be written to handle not only TRUE or FALSE, but also UNKNOWN.
13
Handling NULL in Queries
20761B Handling NULL in Queries 5: Sorting and Filtering Data Different components of SQL Server handle NULL differently Query filters (ON, WHERE, HAVING) filter out UNKNOWNs CHECK constraints accept UNKNOWNS ORDER BY, DISTINCT treat NULLs as equals Testing for NULL Use IS NULL or IS NOT NULL rather than = NULL or <> NULL SELECT custid, city, region, country FROM Sales.Customers WHERE region IS NOT NULL;
14
20761B Lab Review 5: Sorting and Filtering Data What is the difference between filtering using the TOP option, and filtering using the WHERE clause? Question What is the difference between filtering using the TOP option, and filtering using the WHERE clause? Answer The TOP option can only be used to filter results based on the columns specified in the ORDER BY clause; and then it can only be used to return a count of rows (or a percentage of rows) from the top of that result set. There is no support for more complex filters that cannot be expressed in terms of sorting. TOP has no facility for filtering NULL values—NULL values in the columns included in the ORDER BY clause are always returned first. Filters in the WHERE clause have no such limitations; you can specify complex filters and filters that handle NULL.
15
Module Review and Takeaways
20761B Module Review and Takeaways 5: Sorting and Filtering Data Review Question(s) Review Question(s) Question Does the physical order of rows in a SQL Server table guarantee any sort order in queries using the table? Answer No. You have the following query: SELECT p.PartNumber, p.ProductName, o.Quantity FROM Sales.Products AS p LEFT OUTER JOIN Sales.OrderItems AS o ON p.ID = o.ProductID ORDER BY o.Quantity ASC You have one new product that has yet to receive any orders. Will this product appear at the top or the bottom of the results? At the top of the results.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.