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[2]{name:"A"}

Node[3]{name:"B"}

Node[4]{name:"C"}

Try this query live create (_0 {`name`:"D"}) create (_1 {`name`:"E"}) create (_2 {`name`:"A"}) create (_3 {`name`:"B"}) create (_4 {`name`:"C"}) create _2-[:`KNOWS`]->_1 create _2-[:`KNOWS`]->_0 create _2-[:`KNOWS`]->_4 create _2-[:`KNOWS`]->_3 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 identifiers:

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
2 rows

Node[2]{name:"A"}

Node[3]{name:"B"}