Cypher
Updated: September 10, 2025Categories: Query
Printed from:
Cypher Query Language (Neo4j) Cheatsheet
1. Connection and Basic Setup
Neo4j Browser
- Access via
http://localhost:7474/browser - Default credentials:
neo4j/neo4j(change on first login)
Cypher-Shell
Bash
123456# Connect to local database
cypher-shell -u neo4j -p password
# Connect to remote database
cypher-shell -a bolt://hostname:7687 -u neo4j -p password
Language Drivers
Python
12345678# Python (neo4j-driver)
from neo4j import GraphDatabase
driver = GraphDatabase.driver(
"bolt://localhost:7687",
auth=("neo4j", "password")
)
2. Graph Database Concepts
Core Elements
- Node: Entities in the graph (like People, Companies)
- Relationship: Connections between nodes
- Property: Key-value attributes on nodes and relationships
Basic Graph Structure
cypher
123456// Node with label and properties CREATE (:Person {name: 'Alice', age: 30}) // Relationship with type and properties CREATE ()-[:KNOWS {since: 2020}]->()
3. Basic Syntax and Patterns
Query Pattern Structure
cypher
1234MATCH (pattern) WHERE condition RETURN results
4. Creating Nodes and Relationships
CREATE
cypher
12345678910111213// Create a single node CREATE (p:Person {name: 'John', age: 35}) // Create multiple nodes CREATE (p1:Person {name: 'Alice'}) CREATE (p2:Person {name: 'Bob'}) // Create a relationship CREATE (p1)-[:KNOWS]->(p2) // Create node with relationship in one command CREATE (p1:Person {name: 'Alice'})-[:WORKS_AT]->(c:Company {name: 'Tech Corp'})
MERGE (Create or Match)
cypher
12345678910// Create node if not exists, otherwise match MERGE (p:Person {name: 'John'}) ON CREATE SET p.created = timestamp() ON MATCH SET p.lastSeen = timestamp() // Merge with relationship MERGE (p:Person {name: 'Alice'}) MERGE (c:Company {name: 'Google'}) MERGE (p)-[:WORKS_AT]->(c)
5. Querying Patterns
Basic MATCH
cypher
1234567891011// Find all people MATCH (p:Person) RETURN p // Find people named John MATCH (p:Person {name: 'John'}) RETURN p // Complex pattern matching MATCH (p:Person)-[:KNOWS]->(friend:Person) WHERE p.name = 'Alice' RETURN friend.name
WHERE Clauses
cypher
12345678910// Comparison operators MATCH (p:Person) WHERE p.age > 30 AND p.city = 'New York' RETURN p // Pattern-based conditions MATCH (p:Person)-[:WORKS_AT]->(c:Company) WHERE c.name STARTS WITH 'Tech' RETURN p, c
6. Updating Data
SET (Update Properties)
cypher
1234MATCH (p:Person {name: 'John'}) SET p.age = 36 SET p += {email: 'john@example.com'}
REMOVE
cypher
123MATCH (p:Person {name: 'John'}) REMOVE p.age
DELETE
cypher
12345678// Delete node MATCH (p:Person {name: 'John'}) DELETE p // Delete node and its relationships MATCH (p:Person {name: 'John'}) DETACH DELETE p
7. Path Finding and Traversal
Shortest Path
cypher
1234MATCH path = shortestPath((p1:Person)-[:KNOWS*]-(p2:Person)) WHERE p1.name = 'Alice' AND p2.name = 'Bob' RETURN path
Path with Length Constraints
cypher
1234MATCH path = (start:Person)-[:KNOWS*1..3]->(end:Person) WHERE start.name = 'Alice' RETURN path
8. Aggregation Functions
cypher
123456789101112// Count connections MATCH (p:Person)-[:KNOWS]->(friend) RETURN p.name, COUNT(friend) as friendCount // Collect related nodes MATCH (p:Person)-[:WORKS_AT]->(c:Company) RETURN c.name, COLLECT(p.name) as employees // Sum numerical properties MATCH (p:Person) RETURN SUM(p.salary) as totalSalary
9. Conditional Logic
CASE Statement
cypher
12345678MATCH (p:Person) RETURN p.name, CASE WHEN p.age < 20 THEN 'Young' WHEN p.age < 40 THEN 'Middle-aged' ELSE 'Senior' END as ageGroup
COALESCE and EXISTS
cypher
12345678// Use first non-null value RETURN COALESCE(p.nickname, p.name) as displayName // Check property existence MATCH (p:Person) WHERE EXISTS(p.email) RETURN p.name
10. Indexing and Constraints
Create Index
cypher
123456CREATE INDEX ON :Person(name) // Unique constraint CREATE CONSTRAINT ON (p:Person) ASSERT p.email IS UNIQUE
11. Import and Export
CSV Import
cypher
1234567LOAD CSV WITH HEADERS FROM 'file:///users.csv' AS row CREATE (p:Person { name: row.name, age: toInteger(row.age) })
Export to CSV
cypher
1234MATCH (p:Person) WITH p.name as Name, p.age as Age RETURN Name, Age
12. Performance Optimization
Best Practices
- Use indexes on frequently queried properties
- Limit result set size with
LIMIT - Avoid expensive path traversals
- Use
PROFILEto analyze query performance
cypher
123// Analyze query performance PROFILE MATCH (p:Person) RETURN p
13. Common Graph Patterns
Recommendation Engine
cypher
123456// Find friends of friends not yet connected MATCH (p:Person)-[:KNOWS]->(friend)-[:KNOWS]->(recommendation) WHERE NOT (p)-[:KNOWS]->(recommendation) AND p <> recommendation RETURN DISTINCT recommendation
Network Analysis
cypher
123456// Find most connected individuals MATCH (p:Person)-[:KNOWS]-(friend) RETURN p.name, COUNT(DISTINCT friend) as connectionCount ORDER BY connectionCount DESC LIMIT 10
14. Best Practices for Graph Modeling
- Keep labels specific and meaningful
- Use relationships to represent distinct interactions
- Avoid using relationships for data that can be properties
- Model data based on query patterns
- Be consistent with naming conventions
- Use constraints to maintain data integrity
Anti-Patterns to Avoid
- Overly complex relationship types
- Storing redundant information
- Ignoring performance implications
- Not leveraging graph-specific querying
Additional Resources
Continue Learning
Discover more cheatsheets to boost your productivity