Skip to main content

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:
This visual syntax makes queries intuitive and readable.

Part 1: Basic Pattern Matching

MATCH: Find Patterns

Simple node match:
Relationship match:
Multi-hop:
Undirected relationship (match either direction):

WHERE: Filter Results

Pattern predicates:
String operations:
Regular expressions:
List membership:

CREATE: Add Data

Create node:
Create relationship:
Create pattern:

MERGE: Create if not exists

Idempotent create (prevents duplicates):
Merge relationship:

SET: Update Properties

Add label:
Copy properties:

DELETE: Remove Data

Delete node (must delete relationships first!):
Delete relationship:
Conditional delete:

Part 2: Variable-Length Paths

Syntax: *min..max

Friends within 1-3 hops:
Any depth (use carefully!):

Shortest Path

Find shortest path between two nodes:
All shortest paths:

Path Functions


Part 3: Aggregations

Basic Aggregations

GROUP BY (implicit)

Cypher groups by non-aggregated columns:

COLLECT: Aggregate to List

Collect distinct:

UNWIND: List to Rows

Flatten nested data:

Part 4: Advanced Patterns

OPTIONAL MATCH: Left Outer Join

Multiple Patterns

Comma separates independent patterns:
Chained 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:
Check query plan:

2. Filter Early

Bad (filter after expand):
Good (filter before expand):

3. Limit Results

4. Use PROFILE to Find Bottlenecks

Look for:
  • High DB Hits → Optimize
  • NodeByLabelScan → Add index
  • Cartesian Products → Fix query logic

5. Avoid Cartesian Products

Bad (creates all combinations):
Good (connected pattern):

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:
Queries:
  1. Find Alice’s direct friends:
  1. Friends of friends (excluding Alice):
  1. Mutual friends of Alice and Bob:
  1. Average age of Alice’s friends:

Exercise 2: Movie Recommendations

Setup:
Query: Recommend movies Bob hasn’t seen (based on Alice):

Exercise 3: Performance Optimization

Original query (slow):
Optimized:
Verify improvement:

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