What is the difference between multiple MATCH clauses and a comma in a Cypher query?

Gene M picture Gene M · Sep 23, 2015 · Viewed 10.7k times · Source

In a Cypher query language for Neo4j, what is the difference between one MATCH clause immediately following another like this:

MATCH (d:Document{document_ID:2})
MATCH (d)--(s:Sentence)
RETURN d,s

Versus the comma-separated patterns in the same MATCH clause? E.g.:

MATCH (d:Document{document_ID:2}),(d)--(s:Sentence)
RETURN d,s

In this simple example the result is the same. But are there any "gotchas"?

Answer

Michael Hunger picture Michael Hunger · Sep 24, 2015

There is a difference: comma separated matches are actually considered part of the same pattern. So for instance the guarantee that each relationship appears only once in resulting path is upheld here.

Separate MATCHes are separate operations whose paths don't form a single patterns and which don't have these guarantees.