602SQL Documentation Index  

Selection of Records from Query Result

The LIMIT clause in the form:

LIMIT [ offset, ] count
allows you to select a section of records from a query result. The section is defined in this way - a number of records specified by the offset number is skipped (counting from 1) and the following number of records specified by the count number is selected. The query
SELECT * FROM Company 
ORDER BY number
LIMIT 5,10
selects sixth to fifteenth record.

If the offset entry is not specified, no records will be skipped, and the records will be selected from the beginning of the result. LIMIT n is similar to LIMIT 0,n. if the record count in the result (after skipping offset) is lesser than count, then the result will contain less records.

Offset and count must be integer positive numbers. They cannot contain pointers to query columns.

If the record order is not specified in the query, then it's not defined which records will be chosen.

The LIMIT clause (without offset) replaces the TOP clause used in another SQL dialects.


Example: Choose the company with the highest sales:

SELECT SUM(Invoices.Amount) AS sum,Company.Name
    FROM Company, Invoices
    WHERE Invoices.Company=Company.Number
    GROUP BY Company.Name
    ORDER BY sum DESC
    LIMIT 1