Database Reference
In-Depth Information
Working with the RETURN clause
RETURN
is used to return the results of Cypher statements to the program, which is re-
questing Neo4j to execute queries or statements. It is similar to SQL
SELECT
. But in
Cypher it needs to be the last statement of the query.
Except the
CREATE
statement, every other Cypher statement should end with
RETURN
,
otherwise it is treated as an invalid statement and the compiler will throw an error.
There are clauses such as
ORDER BY, SKIP,
and
LIMIT,
which can be used with
RETURN
for getting the results in a desired format.
Let's take an example where we need to get all nodes marked with the label
Artist
in a
particular order (ascending or descending) and also retrieve only the top two rows:
MATCH (n:Artist) return n order by n.Name desc LIMIT 2;
Remove
desc
from the preceding statement to get the results in ascending order. You can
also use
SKIP
with
RETURN
for ignoring a particular number of specified rows starting
from the top of the results.
Let's rewrite our previous query and use to
SKIP
1 record:
MATCH (n:Artist) return n order by n.Name desc SKIP 1 LIMIT
2;
The results of the preceding query will skip a record from the top; then it will show the
second and third rows of the result set, as shown in the following screenshot: