The
TOP
and
OFFSET-FETCH
Filters
Earlier in this chapter, I covered filters that are based on the predicates WHERE and HAVING. In this
section, I cover filters that are based on number of rows and ordering. I’ll start with a filter called TOP
that has been supported in SQL Server for quite some time—since version 7.0. Then I’ll introduce a
new filter called OFFSET-FETCH that was introduced in SQL Server 2012.
The
TOP
Filter
The TOP option is a proprietary T-SQL feature that allows you to limit the number or percentage of
rows that your query returns. It relies on two elements as part of its specification; one is the number
or percent of rows to return, and the other is the ordering. For example, to return from the Orders
table the five most recent orders, you would specify TOP (5) in the SELECT clause and orderdate DESC
in the ORDER BY clause, as shown in Listing 2-5.
LISTING 2-5
Query Demonstrating the TOP Option
SELECT TOP (5) orderid, orderdate, custid, empid
FROM Sales.Orders
ORDER BY orderdate DESC;
www.it-ebooks.info
Do'stlaringiz bilan baham: |