Cypher Query Language Cheat Sheet

Quick reference guide for Cypher & Neo4j: MATCH, CREATE, MERGE, relationship patterns, and graph traversal.

Databases
neo4j
cypher
graph-database

Cypher is a declarative graph query language developed for Neo4j that allows for expressive and efficient querying of graph nodes and relationships.

Node & Relationship Pattern Syntax

cypher
// Node pattern: (variable:Label {property: value})
(u:User {id: 1, name: "Alice"})

// Directed relationship pattern: -[:TYPE]->
(u:User)-[:POSTED]->(p:Post)

// Undirected relationship pattern: -[:TYPE]-
(u1:User)-[:FRIENDS]-(u2:User)

Basic Cypher Statements Reference

cypher
// 1. MATCH & Return Nodes with filter
MATCH (u:User)-[:POSTED]->(p:Post)
WHERE p.created_at >= '2026-01-01'
RETURN u.name, p.title
ORDER BY p.created_at DESC
LIMIT 10;

// 2. CREATE new Node and Relationship
CREATE (u:User {name: "Bob", email: "bob@example.com"})-[:LIKES]->(p:Post {id: 101});

// 3. MERGE (Upsert node or relationship)
MERGE (u:User {email: "bob@example.com"})
ON CREATE SET u.createdAt = timestamp()
ON MATCH SET u.lastLogin = timestamp()
RETURN u;

// 4. DELETE node and its attached relationships
MATCH (u:User {id: 99})
DETACH DELETE u;

Indexing & Constraints

cypher
-- Create Uniqueness Constraint on Node property
CREATE CONSTRAINT FOR (u:User) REQUIRE u.email IS UNIQUE;

-- Create Index on Node property for fast lookup
CREATE INDEX FOR (p:Post) ON (p.title);

Common Pitfalls & Tips

[!WARNING] Attempting to run DELETE u on a node with connected relationships will fail! Use DETACH DELETE u to automatically delete all connected relationships.

[!TIP] Use MERGE instead of CREATE when you want to avoid duplicating existing graph nodes or relationships.