1

This example uses the exists() function:

MATCH (p:Person)
WHERE exists((p)-[:LIVING_IN]->(:Country {name: 'Germany'}))
RETURN p.name
ORDER BY p.name;

How is that query different than:

MATCH (p:Person)-[:LIVING_IN]->(:Country {name: 'Germany'})
RETURN p.name
ORDER BY p.name

Why should I use the exist() function?

I don't understand what is the difference or use case for the exist() function.

cybersam
  • 63,203
  • 6
  • 53
  • 76
Vlasta
  • 141
  • 3

1 Answers1

1

The MATCH clause must look for and remember all matches. But the EXISTS function only needs to find a single pattern match to return TRUE. So EXISTS can speed up a query.

Also, in your first query, no duplicate names will be returned (assuming every person has a different name). But your second query can return duplicate names (since the same person can be found in multiple pattern matches if he has residences in multiple countries).

cybersam
  • 63,203
  • 6
  • 53
  • 76