SKIP
SKIP
(and its synonym OFFSET
) defines from which row to start including the rows in the output.
By using SKIP
, the result set will get trimmed from the top.
Neo4j does not guarantee the results generated by SKIP /OFFSET .
The only clause that guarantees a specific row order is ORDER BY .
|
SKIP
accepts any expression that evaluates to a positive INTEGER
and does not refer to nodes or relationships.
Example graph
The following graph is used for the examples below:
To recreate it, run the following query against an empty Neo4j database:
CREATE
(andy: Person {name: 'Andy'}),
(bernard: Person {name: 'Bernard'}),
(charlotte: Person {name: 'Charlotte'}),
(david: Person {name: 'David'}),
(erika: Person {name: 'Erika'}),
(andy)-[:KNOWS]->(bernard),
(andy)-[:KNOWS]->(charlotte),
(andy)-[:KNOWS]->(david),
(andy)-[:KNOWS]->(erika)
Examples
The following query returns a subset of the result, starting from the fourth result.
MATCH (n)
RETURN n.name
ORDER BY n.name
SKIP 3
n.name |
---|
|
|
Rows: 2 |
The following query returns the middle two rows, with SKIP
skipping the first and LIMIT
removing the final two.
MATCH (n)
RETURN n.name
ORDER BY n.name
SKIP 1
LIMIT 2
n.name |
---|
|
|
Rows: 2 |
SKIP
to return a subset of the rowsSKIP
accepts any expression that evaluates to a positive INTEGER
, as long as it can be statically calculated (i.e. calculated before the query is run).
This query skips the first row and then randomly skips an additional 0, 1, or 2 rows, resulting in skipping a total of 1, 2, or 3 rows before returning the remaining names.
MATCH (n)
RETURN n.name
ORDER BY n.name
SKIP 1 + toInteger(3 * rand())
n.name |
---|
|
|
|
|
Rows: 4 |
Using SKIP
as a standalone clause
SKIP
MATCH (n)
SKIP 2
RETURN collect(n.name) AS names
names |
---|
|
|
The following query orders all nodes by name
, skips the two first rows and limits the results to two rows.
It then collects the results in a list.
SKIP
used in conjunction with ORDER BY
and LIMIT
MATCH (n)
ORDER BY n.name
SKIP 2
LIMIT 2
RETURN collect(n.name) AS names
names |
---|
|
|
OFFSET
as a synonym to SKIP
OFFSET
was introduced as part of Cypher®'s GQL conformance and can be used as a synonym to SKIP
.
MATCH (n)
ORDER BY n.name
OFFSET 2
LIMIT 2
RETURN collect(n.name) AS names
names |
---|
|
|