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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.