3.3.10. LIMIT

LIMIT constrains the number of rows in the output.

3.3.10.1. Introduction

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

Figure 3.16. Graph
alt

3.3.10.2. Return a subset of the rows

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

Query. 

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

The top three items are returned by the example query.

Table 3.104. Result
n.name

3 rows

"A"

"B"

"C"

Try this query live.  CREATE (a {name: 'A'}), (b {name: 'B'}), (c {name: 'C'}), (d {name: 'D'}), (e {name: 'E'}), (a)-[:KNOWS]->(b), (a)-[:KNOWS]->(c), (a)-[:KNOWS]->(d), (a)-[:KNOWS]->(e) MATCH (n) RETURN n.name ORDER BY n.name LIMIT 3

3.3.10.3. Using an expression with LIMIT to return a subset of the rows

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

Query. 

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

Returns one to three top items.

Table 3.105. Result
n.name

3 rows

"A"

"B"

"C"

Try this query live.  CREATE (a {name: 'A'}), (b {name: 'B'}), (c {name: 'C'}), (d {name: 'D'}), (e {name: 'E'}), (a)-[:KNOWS]->(b), (a)-[:KNOWS]->(c), (a)-[:KNOWS]->(d), (a)-[:KNOWS]->(e) MATCH (n) RETURN n.name ORDER BY n.name LIMIT toInt(3 * rand()) + 1