Skip to main content
Version: v0.1.0

Cypher Query Guide

Cypher is a declarative query language for graph databases. Just as SQL works with relational databases, Cypher allows you to query and manipulate graph nodes and relationships using intuitive pattern syntax.

You can execute Cypher queries directly in D.Hub's Graph Explorer to explore ontology data.

Basic Syntax

MATCH — Pattern Matching

Finds data in the graph that matches a specific pattern.

MATCH (n)
RETURN n
LIMIT 25

WHERE — Conditional Filtering

Applies conditions to results found by MATCH.

MATCH (p:Person)
WHERE p.age >= 30
RETURN p.name, p.age

RETURN — Returning Results

Specifies the shape of the data to be returned. Aliases (AS) can be used.

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

ORDER BY — Sorting

Sorts results by a specific property.

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

LIMIT — Limiting Result Count

Limits the number of results returned. Strongly recommended when querying large datasets.

MATCH (n)
RETURN n
LIMIT 100

Node Patterns

Nodes are represented with parentheses (). You can specify labels and properties.

PatternDescription
(n)Any node
(p:Person)A node with the Person label
(p:Person {name: "Alice"})A node with a specific property value
(n:Person:Employee)A node with multiple labels

Relationship Patterns

Relationships are represented with brackets [] and arrows ->, <-.

PatternDescription
(a)-[r]->(b)Relationship from a to b
(a)<-[r]-(b)Relationship from b to a
(a)-[r]-(b)Direction-agnostic
(a)-[:BOUGHT]->(b)BOUGHT type relationship
(a)-[*1..3]->(b)Variable-length relationship of 1 to 3 hops

Commonly Used Query Patterns

1. Retrieve All Nodes

MATCH (n)
RETURN n
LIMIT 100

2. Retrieve Nodes by Label

MATCH (p:Person)
RETURN p
LIMIT 50

3. Filter by Property Conditions

MATCH (p:Product)
WHERE p.price > 10000 AND p.category = "Electronics"
RETURN p.name, p.price
ORDER BY p.price DESC

4. Explore Connections Through Relationships

MATCH (c:Customer)-[:PURCHASED]->(p:Product)
RETURN c.name, p.name
LIMIT 50

5. Multi-Relationship Path Exploration

MATCH (a:Person)-[:WORKS_FOR]->(company:Organization)-[:LOCATED_IN]->(city:City)
RETURN a.name, company.name, city.name

6. Finding the Shortest Path

MATCH path = shortestPath(
(a:Person {name: "Alice"})-[*]-(b:Person {name: "Bob"})
)
RETURN path

7. N-Hop Neighbor Query

MATCH (start:Person {name: "Alice"})-[*1..2]-(neighbor)
RETURN DISTINCT neighbor
LIMIT 50

8. Aggregation — Counting Nodes

MATCH (n)
RETURN labels(n) AS label, count(*) AS count
ORDER BY count DESC

9. Aggregation — Statistics by Relationship

MATCH ()-[r]->()
RETURN type(r) AS relationship, count(*) AS count
ORDER BY count DESC

10. Top N Results by Group

MATCH (c:Customer)-[:PURCHASED]->(p:Product)
RETURN c.name, count(p) AS purchase_count
ORDER BY purchase_count DESC
LIMIT 10

11. Checking Pattern Existence

MATCH (p:Person)
WHERE EXISTS {
MATCH (p)-[:PURCHASED]->(:Product {category: "Books"})
}
RETURN p.name

12. Collecting and Using Lists

MATCH (c:Customer)-[:PURCHASED]->(p:Product)
RETURN c.name, collect(p.name) AS purchased_products
LIMIT 20

Useful Built-in Functions

FunctionDescriptionExample
count()Count itemscount(n)
collect()Collect results into a listcollect(n.name)
labels()Get node labelslabels(n)
type()Get relationship typetype(r)
properties()Get all properties as a mapproperties(n)
size()List sizesize(collect(n))
coalesce()Return fallback value when NULLcoalesce(n.name, "Unknown")
toUpper()Convert to uppercasetoUpper(n.name)
toString()Convert to stringtoString(n.age)
date()Create a datedate("2025-01-01")

Tips for Using Graph Explorer

Leveraging Result Views

In Graph Explorer, query results can be viewed in three modes: Graph, Table, and Text. Graph view is best for understanding structure, table view for data analysis, and text view for debugging.

  • Make LIMIT a habit: Visualizations can become sluggish when a large number of nodes are returned. Always include LIMIT.
  • Use the metadata panel: Click labels or relationship types in the left panel to immediately query data of that type.
  • Expanded exploration: Double-click a node in the graph view to retrieve additional connected neighboring nodes.
Performance Warning

Queries like MATCH (n) RETURN n that retrieve the entire graph can impact browser performance when there is a lot of data. Always specify a LIMIT or add label conditions.