Limiting the number of rows returned from a query

You can create a report that contains a limited number of ordered entries.

Procedure

  1. Construct an SQL query that selects and orders data as appropriate to your objective.
  2. Run the query and specify the row limit value that gives the result you want.

    The key elements are the ORDER BY clause of the SQL statement and the ROWLIMIT parameter of the RUN QUERY command.

Example

For example, to create a report that contains the five managers with the most years of service, you could use the following query and QMF command.

SQL query:

SELECT NAME, YEARS
  FROM Q.STAFF
 WHERE JOB='MGR'
 ORDER BY YEARS DESC

QMF command:

RUN QUERY (ROWLIMIT=5

Resulting report:

NAME        YEARS
---------  ------
JONES          12
QUILL          10
HANES          10
LU             10
LEA             9