Top Values Queries

You use the TOP clause to limit the number of rows that SQL Server includes in the output. Here's an example:

SELECT TOP 10 OrderDate, Sum(Freight) FROM Orders 
     GROUP BY OrderDate
     ORDER BY SUM(Freight) DESC

This example shows the 10 order dates with the highest total freight (see Figure 6.18).

Figure 6.18. A SELECT statement that shows the 10 order dates with the highest total freight.

In addition to allowing you to select the top number of rows, T-SQL also allows you to select the top percent of rows. Here's an example:

 SELECT TOP 10 PERCENT OrderDate, Sum(Freight) FROM Orders GROUP BY OrderDate ORDER ...

Get Alison Balter's Mastering Access 2002 Enterprise Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.