Chapter 6 Query Language Components : Limit Clause

Limit Clause
You can use an optional limit clause in a select or an order by clause.
When used in a select clause, the limit the maximum number of rows to return.
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 Implicit Window Examples.
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

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

 
The above 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.
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.