Search code examples
cyphergraph-databasesmemgraphdb

Can I have two RETURN clauses in Cypher query?


I'm going through my first tutorial on Memgraph Playgound. In one od the lessons there is the following code:

MATCH (jon:Character { name: "Jon Snow" })-[killed:KILLED]->(character:Character)
RETURN jon, killed, character;

From the graph and the table, I can see that result returns 17 nodes.

I've modified the query to get the number of nodes instead of list of nodes:

MATCH (jon:Character { name: "Jon Snow" })-[killed:KILLED]->(character:Character)
RETURN count(jon);

Is it possible to have a query that would return both results at the same time, the count of nodes, as well as individual nodes? I've tried something like the following code but it doesn't work:

MATCH (jon:Character { name: "Jon Snow" })-[killed:KILLED]->(character:Character)
RETURN jon, killed, character;
RETURN count(jon);

Can you have more than one RETURN in a single query?


Solution

  • You can use the WITH clause to pass the results from one part of the query to another. The WITH clause allows you to use the results of the first part of the query in the second part.

    MATCH (jon:Character { name: "Jon Snow" })-[killed:KILLED]->(character:Character)
    WITH jon, killed, character, count(jon) as node_count
    RETURN jon, killed, character, node_count;