0% found this document useful (0 votes)
3 views4 pages

Neo4j Queries Guide

This document is a quick reference guide for Neo4j queries, covering basic commands for creating nodes and relationships, filtering data, updating and deleting nodes, and returning specific properties. It also includes sections on sorting, aggregate functions, pattern matching, creating complex graphs, using parameters, indexing, constraints, and graph algorithms. The guide provides essential commands and examples for efficiently working with Neo4j databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Neo4j Queries Guide

This document is a quick reference guide for Neo4j queries, covering basic commands for creating nodes and relationships, filtering data, updating and deleting nodes, and returning specific properties. It also includes sections on sorting, aggregate functions, pattern matching, creating complex graphs, using parameters, indexing, constraints, and graph algorithms. The guide provides essential commands and examples for efficiently working with Neo4j databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Neo4j Queries - Quick Reference Guide

1. Basic Commands

• Create a Node:

CREATE (p:Person {name: 'John', age: 30})

• Create a Relationship:

MATCH (a:Person {name: 'John'}), (b:Person {name: 'Jane'})


CREATE (a)-[:FRIEND]->(b)

• Return All Nodes:

MATCH (n) RETURN n

• Return Nodes with Label:

MATCH (p:Person) RETURN p

2. Filter & WHERE Clause

• Filter by Property:

MATCH (p:Person) WHERE p.age > 25 RETURN p

• Multiple Conditions:

MATCH (p:Person) WHERE p.age > 25 AND p.name = 'John' RETURN p

3. Updating Data

• Update a Node:

1
MATCH (p:Person {name: 'John'}) SET p.age = 35

• Add Property:

MATCH (p:Person {name: 'John'}) SET p.city = 'New York'

• Remove Property:

MATCH (p:Person {name: 'John'}) REMOVE p.city

4. Deleting Data

• Delete a Relationship:

MATCH (a)-[r:FRIEND]->(b) DELETE r

• Delete a Node (no relationships):

MATCH (p:Person {name: 'John'}) DELETE p

• Delete a Node with Relationships:

MATCH (p:Person {name: 'John'}) DETACH DELETE p

5. Return Specific Properties

MATCH (p:Person) RETURN p.name, p.age

6. Sorting & Limiting

• Order by Property:

MATCH (p:Person) RETURN p ORDER BY p.age DESC

• Limit Results:

2
MATCH (p:Person) RETURN p LIMIT 5

7. Aggregate Functions

• Count:

MATCH (p:Person) RETURN COUNT(p)

• Average, Max, Min:

MATCH (p:Person) RETURN AVG(p.age), MAX(p.age), MIN(p.age)

8. Pattern Matching

• Match Relationship:

MATCH (a:Person)-[:FRIEND]->(b:Person) RETURN a, b

• Match Relationship Both Directions:

MATCH (a:Person)--(b:Person) RETURN a, b

9. Creating Complex Graphs

CREATE (a:Person {name: 'Alice'})-[:FRIEND]->(b:Person {name: 'Bob'})-


[:COLLEAGUE]->(c:Person {name: 'Charlie'})

10. Using Parameters (for APIs)

:param name => 'Alice'


MATCH (p:Person {name: $name}) RETURN p

3
11. Indexing & Constraints

• Create Index:

CREATE INDEX FOR (p:Person) ON (p.name)

• Create Unique Constraint:

CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE

12. Graph Algorithms (using APOC)

• PageRank Example:

CALL algo.pageRank.stream('Person', 'FRIEND')


YIELD nodeId, score
RETURN algo.getNodeById(nodeId).name AS name, score
ORDER BY score DESC

Note: Requires APOC plugin to be installed.

Let me know if you want this converted to PDF or want advanced queries for real-world use cases.

You might also like