Neo4j Cypher Reference

beginner
6 min

Neo4j Cypher Reference

Welcome to our comprehensive guide on Neo4j Cypher, a powerful and easy-to-use graph query language that allows you to traverse and query your data within Neo4j databases. Let's embark on this exciting journey together!

What is Neo4j Cypher?

Neo4j Cypher is the declarative graph query language that Neo4j uses to interact with its graph database. It's designed to simplify the process of working with complex, interconnected data. Think of it as a powerful tool that helps you navigate through a vast network of data, making it easy to find the information you need.

Why Neo4j Cypher?

Neo4j Cypher is chosen for its simplicity, scalability, and high performance. It allows you to perform complex graph operations in a straightforward manner, making it an ideal choice for real-world applications.

Getting Started with Neo4j Cypher

Basic Cypher Syntax

Creating Nodes

💡 Pro Tip: In Neo4j, nodes represent individual items in a graph, like people, places, or things.

cypher
CREATE (n:Label)

Replace Label with the type of node you want to create.

Creating Relationships

📝 Note: Relationships in Neo4j represent the connections between nodes.

cypher
CREATE (n1:Label1)-[:RELATIONSHIP_TYPE]->(n2:Label2)

Replace LABEL1 and LABEL2 with the types of the connecting nodes, and RELATIONSHIP_TYPE with the relationship type.

Querying Data

cypher
MATCH (n:Label) RETURN n

Replace Label with the type of the node you want to query.

Advanced Cypher Features

Pattern Matching

cypher
MATCH (n1:Label1)-[:RELATIONSHIP_TYPE*]->(n2:Label2) RETURN n1, n2

This query finds all paths from a Label1 node to a Label2 node, regardless of the number of intermediary relationships.

Filtering Results

cypher
MATCH (n:Label) WHERE n.property = 'value' RETURN n

Filter the results based on a property of the node.

🎯 Practical Example

Consider a social network where users can follow each other. Here's how you might create and query this data using Neo4j Cypher:

cypher
CREATE (user1:User {name: 'Alice', age: 30}) CREATE (user2:User {name: 'Bob', age: 25}) CREATE (user3:User {name: 'Charlie', age: 22}) CREATE (user1)-[:FOLLOWS]->(user2) CREATE (user1)-[:FOLLOWS]->(user3) MATCH (user:User) WHERE user.age > 20 RETURN user

This creates three users and connects them with FOLLOWS relationships. It then queries for users older than 20.

Quick Quiz
Question 1 of 1

Which Cypher query creates a node of type `User` with the name 'John' and age 28?

Happy coding! 💡 📝 🎯