Finding Nodes that do not have a relationship

I don't know why this seems so difficult to figure out. I'm working with the following Nodes and relationships:
image

I have this code:
MATCH

(n:Notification)-[cfn:CONTACT_FIRST_NAME]->(fn:Name)<-[hfn:HAS_FIRST_NAME]-(c:Contact),

(n)-[cln:CONTACT_LAST_NAME]->(ln:Name)<-[hln:HAS_LAST_NAME]-(c)

RETURN *

And it gives me the first and last names of those who have received a notification and are also a "Contact", but I want to find the names that received a notification, but are not a name owned by a "Contact"

I tried using the WHERE NOT clause, but it didn't yield the proper results. Please help me!

Hi @gq16

How about this?

I created two person data.

CREATE (n:Notification {id: 1})-[:CONTACT_FIRST_NAME]->(:Name {name:"Gehad"})<-[:HAS_FIRST_NAME]-(c:Contact),
(n)-[:CONTACT_LAST_NAME]->(:Name {name:"Qaki"})<-[:HAS_LAST_NAME]-(c);
CREATE (n:Notification {id: 2})-[:CONTACT_FIRST_NAME]->(:Name {name:"Koji"}),
(n)-[:CONTACT_LAST_NAME]->(:Name {name:"Annoura"});

The query is like this.

MATCH
(n:Notification)-[cfn:CONTACT_FIRST_NAME]->(fn:Name),
(n)-[cln:CONTACT_LAST_NAME]->(ln:Name)
WHERE NOT (fn)<-[:HAS_FIRST_NAME]-(:Contact)-[:HAS_LAST_NAME]->(ln)
RETURN *

2 Likes

Incredible! Thank you for your help Koji!