LIMIT

Introduction

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

Graph
  N0 [
    label = "name = \'A\'\l"
  ]
  N0 -> N4 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N0 -> N3 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N0 -> N2 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N0 -> N1 [
    color = "#2e3436"
    fontcolor = "#2e3436"
    label = "KNOWS\n"
  ]
  N1 [
    label = "name = \'B\'\l"
  ]
  N2 [
    label = "name = \'C\'\l"
  ]
  N3 [
    label = "name = \'D\'\l"
  ]
  N4 [
    label = "name = \'E\'\l"
  ]

Return a limited subset of the rows

To return a limited subset of the rows, use this syntax:

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

Limit to 3 rows by the example query.

Table 1. Result
n.name

"A"

"B"

"C"

Rows: 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 1 + toInteger(3 * rand())

Limit 1 row plus randomly 0, 1, or 2. So randomly limit to 1, 2, or 3 rows.

Table 2. Result
n.name

"A"

"B"

Rows: 2

LIMIT will not stop side effects

The use of LIMIT in a query will not stop side effects, like CREATE, DELETE or SET, from happening if the limit is in the same query part as the side effect. This behaviour was undefined in versions before 4.3.

Query
CREATE (n)
RETURN n
LIMIT 0

This query returns nothing, but creates one node:

Table 3. Result

(empty result)

Rows: 0
Nodes created: 1

Query
MATCH (n {name: 'A'})
SET n.age = 60
RETURN n
LIMIT 0

This query returns nothing, but writes one property:

Table 4. Result

(empty result)

Rows: 0
Properties set: 1

If we want to limit the number of updates we can split the query using the WITH clause:

Query
MATCH (n)
WITH n LIMIT 1
SET n.locked = true
RETURN n

Writes locked property on one node and return that node:

Table 5. Result
n

Node[0]{locked:true,name:"A"}

Rows: 1
Properties set: 1