Neo4j Cypher Cheatsheet - Neo4j Graph Database Command Reference

All essential Neo4j Cypher commands organized by use case, with 40+ entries you can copy and run directly. Find the right command fast when you need it.

Databases·40 commands·Last updated 2026-07-21
Back to Databases

Create Nodes & Relationships 5

CREATE (n:Person {name: "Alice"})
Create a node
CREATE (a:Person)-[:KNOWS]->(b:Person)
Create a relationship
CREATE (a:Person {name: "Alice"})-[:KNOWS {since: 2020}]->(b:Person {name: "Bob"})
Create node & relationship with properties
MERGE (n:Person {name: "Alice"})
Match if exists, else create
MERGE (a)-[:KNOWS]->(b) ON CREATE SET a.created = timestamp()
MERGE with onCreate

Query & Match 9

MATCH (n:Person) RETURN n
Match all Person nodes
MATCH (n:Person {name: "Alice"}) RETURN n
Match by property
MATCH (n:Person) WHERE n.age > 18 RETURN n
WHERE filter
MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a, b
Match a relationship
MATCH (a:Person)-[:KNOWS*2]->(b:Person) RETURN a, b
2-hop relationship
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person) RETURN a, b
1-3 hop relationship
MATCH (n) OPTIONAL MATCH (n)-[:KNOWS]->(m) RETURN n, m
Optional match (null if no rel)
MATCH path = (a)-[*]->(b) RETURN path
Match a path
MATCH path = shortestPath((a)-[:KNOWS*]->(b)) RETURN path
Shortest path

WITH & Subqueries 5

MATCH (n:Person) WITH n WHERE n.age > 18 RETURN n.name
WITH filter & pass
MATCH (n:Person)-[:KNOWS]->(m) WITH n, count(m) AS friends RETURN n.name, friends
WITH aggregate & pass
UNWIND [1, 2, 3] AS x RETURN x
Unwind array into rows
CALL { MATCH (n:Person) RETURN n ORDER BY n.name LIMIT 5 } RETURN count(*)
CALL subquery
MATCH (n:Person) RETURN n.name AS name UNION MATCH (n:Company) RETURN n.name AS name
UNION merge results

Update & Delete 6

MATCH (n:Person {name: "Alice"}) SET n.age = 30
Update a property
MATCH (n:Person {name: "Alice"}) SET n += {age: 30, city: "Beijing"}
Update multiple props
MATCH (n:Person {name: "Alice"}) REMOVE n.age
Remove a property
MATCH (n:Person {name: "Alice"}) DELETE n
Delete node (must have no rels)
MATCH (n:Person {name: "Alice"}) DETACH DELETE n
Delete node and its relationships
MATCH (a)-[r:KNOWS]->(b) DELETE r
Delete a relationship

Aggregation & Order 9

MATCH (n:Person) RETURN count(n)
Count
MATCH (n:Person) RETURN DISTINCT n.city
Distinct
MATCH (n:Person) RETURN n.name, n.age ORDER BY n.age DESC
Order by
MATCH (n:Person) RETURN n.age, count(*) ORDER BY n.age
Group & count
MATCH (n:Person) RETURN collect(n.name)
Collect into array
MATCH (n:Person) RETURN avg(n.age), min(n.age), max(n.age)
Aggregate functions
MATCH (n:Person) RETURN percentileCont(n.age, 0.5)
Median (percentile)
MATCH (n:Person) RETURN n LIMIT 10
Limit results
MATCH (n:Person) RETURN n SKIP 5 LIMIT 10
Pagination

Index & Constraint 6

CREATE INDEX ON :Person(name)
Create an index
CREATE CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUE
Unique constraint
CREATE CONSTRAINT ON (n:Person) ASSERT exists(n.name)
Existence constraint
DROP INDEX ON :Person(name)
Drop an index
DROP CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUE
Drop a constraint
CALL db.indexes()
List all indexes

💡 Tips

  • MATCH 查询前先用 EXPLAIN 查看执行计划,避免全图扫描。
  • MERGE 比 CREATE 安全,避免重复创建节点。
  • 长路径查询(*1..10)可能很慢,建议加 LIMIT 或用图算法。

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it