cancel
Showing results forΒ
Did you mean:Β

Head's Up! Site migration is underway. Phase 2: migrate recent content

## Optional Match Returning No Records

Node

Data Model along with Count's is as follows:
Nodes: A(40),B(100)
Relationship: AA(self-relation with nodes of label A) count: 36, AB(from A to B) count:0

Query1:

``````Match (p:A)-[b:AA]->(p1)
OPTIONAL MATCH (p1)-[d:AB]->(c:B)
RETURN p,p1,b,d,c
``````

Output: A(40), AA(36), AB(0) and B(0). The output is the same as the expected output

Query2:

`````` MATCH (p1:A)-[d:AB]->(c:B)
OPTIONAL Match (p)-[b:AA]->(p1)
RETURN p,p1,b,d,c
``````

This query is prepared by rearranging the sequence in the first query.

Output By Neo4j: (no changes, no records)

Expected Output: A and AA data, as they are present in the graph even if the first sequence is not present.

Request you to go through the queries and suggest your thoughts, to return the data even if some patterns return no records.

Thanks & Regards,
Vinayak

3 REPLIES 3
Ninja

If I am interpreting your explanation correctly, the results of query1 shows there are no nodes B connected to nodes A, for nodes A that have a self relationship. If this is correct, the match statement in query2 would produce no records for all nodes A that have a self-relationship. The other option is nodes B related to nodes A, for those nodes A that do not have a self-relationship. There must not be any of those as well.

Your interpretation is correct. But for query 2, we need the data present which is A and AA. Please suggest changes required to get the expected output for query2.

Ninja

You can try this. It will always give a result for an existing value of A, with missing values returned as null.

MATCH (p1:A)
OPTIONAL MATCH (p1)-[d:AB]->(c:B)
OPTIONAL MATCH (p)-[b:AA]->(p1)
RETURN p,p1,b,d,c

Nodes 2022

NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online

Neo4j Resources