Limit Clause

You can use an optional limit clause in a select or an order by clause.

When used in a select clause, it limits the maximum number of rows to return. The limit clause is applied last after the all the clauses are executed on the result set.

You can also use an optional offset to ignore the first n rows.

When used in an ordered by clause, the limit applies to each of the items in the ordered list (after the ordering is executed). See Working With Implicit Windows.

Example Showing Use in Select Clause

select {limit: first 10 offset 20} c.name from /Customer c

Without the limit clause, this query would return all customers. With the limit, it returns 10 customers, with an offset of 20. That is, it returns customers 20-30.

Example Showing Use in Order By Clause

The following query keeps count of the number of students per department. Every time a student enrolls or leaves, the count changes and the query produces the entire list sorted on the count, sorted in descending order, and limited to the first two.

select s.deptName, count(*)
  from /Student s
  group by s.deptName
  order by count(*) desc {limit: first 2};

The limit clause specifies that only the first two of the ordered lists of departments are returned by the query: the list of departments with the largest number of students, and the list of departments with the second largest number of students.