The
LIMIT
expression allows one to limit the number of result set rows in a query. For example:
SELECT contact_id, last_name, first_name FROM contacts WHERE website = 'TechOnTheNet.com' ORDER BY contact_id DESC LIMIT 5;
will select the first
5
records from the contacts table where the website isTechOnTheNet.com
. Note that the results are sorted bycontact_id
in descending order so this means that the 5 largestcontact_id
values will be returned. If there are other records in the contacts table that have a website value ofTechOnTheNet.com
, they will not be returned.If we wanted to select the 5 smallest
contact_id
values instead of the largest, we could change the sort order as follows:SELECT contact_id, last_name, first_name FROM contacts WHERE website = 'TechOnTheNet.com' ORDER BY contact_id ASC LIMIT 5;Here the results would be sorted by
contact_id
in ascending order. So the first 5 smallestcontact_id
records that have a website ofTechOnTheNet.com
would be returned, and no other records would be returned by this query.Limit with Offset
Though the
OFFSET
keyword is not supported by eXtremeSQL, the notion of theoffset
functionality is supported using theLIMIT
expression. For example:
SELECT... FROM table LIMIT M,N;
will select the
N
records starting from line numberM
(whereM
is zero-based); in other words skipM
result set rows. Thus
SELECT... FROM table LIMIT 0,N;
is equivalent to:
SELECT... FROM table LIMIT N;