Use OPTIONAL MATCH to find patterns that might not exist, returning empty values when patterns don’t match. The OPTIONAL MATCH clause works like MATCH but allows parts of the pattern to be missing. It preserves nulls in the result when the pattern does not fully match, similar to a left outer join in
relational databases.
Suppose we are interested all the persons who have income of greater and 185000 and not live in Capitola.
MATCH (resident:person WHERE resident.income > 185000)OPTIONAL MATCH (resident)-[:lives_is]->(c:city) WHERE c.name <> 'Capitola' RETURN p.name as Person, c.name as City
This query finds all Person nodes where the income is greater than 185000, and optionally matches cities they’re connected to (excluding Capitola). If a person isn’t connected to any city, the City column returns empty, but the person is still included in the results.OutputThe optional match ensures that people without city connections are still returned