Cypher Query Language Mastery
Module Duration: 6-8 hours
Learning Style: Hands-On + Pattern Recognition + Real-World Queries
Outcome: Write efficient, expressive Cypher queries for any graph problem
Introduction: ASCII Art for Graphs
Cypher uses ASCII art to represent graph patterns:Part 1: Basic Pattern Matching
MATCH: Find Patterns
Simple node match:WHERE: Filter Results
CREATE: Add Data
Create node:MERGE: Create if not exists
Idempotent create (prevents duplicates):SET: Update Properties
DELETE: Remove Data
Delete node (must delete relationships first!):Part 2: Variable-Length Paths
Syntax: *min..max
Friends within 1-3 hops:
Shortest Path
Find shortest path between two nodes:Path Functions
Part 3: Aggregations
Basic Aggregations
GROUP BY (implicit)
Cypher groups by non-aggregated columns:COLLECT: Aggregate to List
UNWIND: List to Rows
Part 4: Advanced Patterns
OPTIONAL MATCH: Left Outer Join
Multiple Patterns
Comma separates independent patterns:NOT Pattern
EXISTS Subquery (Neo4j 4.0+)
CASE Expressions
Part 5: Functions
String Functions
Math Functions
List Functions
Date/Time Functions
Graph Functions
Part 6: Performance Optimization
1. Use Indexes
Create index:2. Filter Early
Bad (filter after expand):3. Limit Results
4. Use PROFILE to Find Bottlenecks
- High DB Hits → Optimize
- NodeByLabelScan → Add index
- Cartesian Products → Fix query logic
5. Avoid Cartesian Products
Bad (creates all combinations):Part 7: Common Patterns
Pattern 1: Recommendations
“People like you who bought X also bought Y”:Pattern 2: Shortest Path
“How are Alice and Bob connected?”:Pattern 3: Influence/Centrality
“Who has the most friends?” (degree centrality):Pattern 4: Community Detection
“Find groups of mutual friends” (triangles):Pattern 5: Hierarchical Data
“Find all employees under a manager” (recursive):Pattern 6: Time-Series Analysis
“What did users buy in the last 30 days?”:Part 8: Hands-On Exercises
Exercise 1: Social Network Queries
Setup:- Find Alice’s direct friends:
- Friends of friends (excluding Alice):
- Mutual friends of Alice and Bob:
- Average age of Alice’s friends:
Exercise 2: Movie Recommendations
Setup:Exercise 3: Performance Optimization
Original query (slow):Summary
Pattern Matching: Use ASCII art for intuitive graph patterns Filtering: WHERE clause for predicates, pattern-based filtering Aggregations: count, avg, sum, collect for grouped data Variable Paths:*min..max for flexible traversals
Functions: Rich library for strings, math, lists, dates
Optimization: Indexes, early filtering, PROFILE for analysis
Next Steps: Apply Cypher to real-world data modeling scenarios!
What’s Next?
Module 5: Graph Data Modeling
Design efficient graph schemas, handle many-to-many relationships, and model complex domains