Some states do not share any borders with another US state. Let's add Alaska to our test graph:
CREATE (CA:State {code: "AK", name: "Alaska", population: 700000 })
In the case of Alaska, the query we wrote before to get the neighbors will actually return zero results:
MATCH (n:State {code: "AK"})-[:SHARE_BORDER_WITH]-(m)
RETURN n, m
Indeed, no pattern matches the sequence ("AK")-SHARE_BORDER_WITH-().
In some cases, we might want to see Alaska in the results anyway. For instance, knowing that Alaska has zero neighbors is information in itself. In that case, we would use OPTIONAL MATCH pattern matching:
MATCH (n:State {code: "AK"})
OPTIONAL MATCH (n)-[:SHARE_BORDER_WITH]-(m)
RETURN n.name, m.name
This query returns the following result:
╒════════╤════════╕
│"n.name"...