You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Cypher is Neo4j's declarative query language, designed to be visual and intuitive. Cypher uses ASCII-art patterns to describe graph structures, making queries readable even without deep database expertise.
Nodes are represented with parentheses:
() // Anonymous node
(p) // Node bound to variable p
(:Person) // Node with label Person
(p:Person) // Node with label, bound to variable p
(p:Person {name: "Alice"}) // Node with label and property filter
Relationships are represented with square brackets and arrows:
--> // Anonymous directed relationship
-[r]-> // Relationship bound to variable r
-[:PURCHASED]-> // Relationship with type PURCHASED
-[r:PURCHASED]-> // Relationship with type, bound to variable
-[r:PURCHASED {date: "2024-01-15"}]-> // With property filter
Combine nodes and relationships into a pattern:
(alice:Person {name: "Alice"})-[:PURCHASED]->(product:Product)
The MATCH clause finds patterns in the graph:
MATCH (p:Person)
RETURN p
MATCH (p:Person {name: "Alice"})
RETURN p
MATCH (p:Person)-[:PURCHASED]->(product:Product)
RETURN p.name, product.name
// Friends of friends
MATCH (p:Person {name: "Alice"})-[:FRIENDS_WITH]->()-[:FRIENDS_WITH]->(fof)
RETURN fof.name
The WHERE clause adds conditions to your query:
MATCH (p:Person)
WHERE p.age > 25
RETURN p.name, p.age
MATCH (p:Person)
WHERE p.name STARTS WITH "A"
RETURN p.name
MATCH (p:Person)
WHERE p.name CONTAINS "li"
RETURN p.name
MATCH (p:Person)
WHERE p.name =~ "A.*" // Regular expression
RETURN p.name
MATCH (p:Person)
WHERE p.age > 25 AND p.name <> "Bob"
RETURN p.name
// Nodes with an email property
MATCH (p:Person)
WHERE p.email IS NOT NULL
RETURN p.name
// Nodes that have a specific relationship
MATCH (p:Person)
WHERE EXISTS { (p)-[:PURCHASED]->(:Product) }
RETURN p.name
MATCH (p:Person)
WHERE p.age IN [25, 30, 35]
RETURN p.name
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.