Contents Index The ORDER BY clause: sorting query results ORDER BY and GROUP BY

ASA SQL User's Guide
  Summarizing, Grouping and Sorting Query Results
    The ORDER BY clause: sorting query results

Explicitly limiting the number of rows returned by a query


You can use the FIRST or TOP keywords to limit the number of rows included in the result set of a query. These keywords are for use with queries that include an ORDER BY clause.

Examples 

The following query returns information about the employee that appears first when employees are sorted by last name:

SELECT FIRST *
FROM employee
ORDER BY emp_lname

The following query returns the first five employees as sorted by last name:

SELECT TOP 5 *
FROM employee
ORDER BY emp_lname

When you use TOP, you can also use START AT to provide an offset. The following statement lists the fifth and sixth employees sorted in descending order by last name:

SELECT TOP 2 START AT 5 *
FROM employee
ORDER BY emp_lname DESC

FIRST and TOP should be used only in conjunction with an ORDER BY clause to ensure consistent results. Use of FIRST or TOP without an ORDER BY triggers a syntax warning, and will likely yield unpredictable results.


Contents Index The ORDER BY clause: sorting query results ORDER BY and GROUP BY