10.3. Limit

LIMIT constrains the number of rows in the output.

LIMIT accepts any expression that evaluates to a positive integer — however the expression cannot refer to nodes or relationships.

Figure 10.3. Graph

Return first part

To return a subset of the result, starting from the top, use this syntax:

Query 

MATCH (n)
RETURN n
ORDER BY n.name
LIMIT 3

The top three items are returned by the example query.

Result

n
3 rows

Node[0]{name:"A"}

Node[1]{name:"B"}

Node[2]{name:"C"}

Try this query live create (_0 {`name`:"A"}) create (_1 {`name`:"B"}) create (_2 {`name`:"C"}) create (_3 {`name`:"D"}) create (_4 {`name`:"E"}) create (_0)-[:`KNOWS`]->(_4) create (_0)-[:`KNOWS`]->(_3) create (_0)-[:`KNOWS`]->(_2) create (_0)-[:`KNOWS`]->(_1) ; match (n) return n order by n.name limit 3

Return first from expression

Limit accepts any expression that evaluates to a positive integer as long as it is not referring to any external variables:

Parameters 

{
  "p" : 12
}

Query 

MATCH (n)
RETURN n
ORDER BY n.name
LIMIT toInt(3 * rand())+ 1

Returns one to three top items

Result

n
3 rows

Node[0]{name:"A"}

Node[1]{name:"B"}

Node[2]{name:"C"}