Skip to content

First Cypher Queries

Tutorial

First Cypher Queries

Learn MATCH, RETURN, and WHERE clauses

30 min Beginner
CypherMATCHWHERERETURN

What You'll Learn

  • MATCH - Find patterns in the graph
  • RETURN - Specify what to return
  • WHERE - Filter results with conditions
  • LIMIT - Control result size
Info:
Prerequisites
Complete 01_property_graphs before this tutorial.
# Cell 1 — Parameters
USERNAME = "_FILL_ME_IN_" # Set your email before running
# Cell 2 — Connect
from graph_olap import GraphOLAPClient
client = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provision
from notebook_setup import provision
personas, conn = provision(USERNAME)
analyst = personas["analyst"]
admin = personas["admin"]
ops = personas["ops"]
client = analyst
print(f"Connected | {conn.query_scalar('MATCH (n) RETURN count(n)')} nodes")
1

MATCH and RETURN

Finding nodes and selecting output

Every Cypher query starts with MATCH, which describes the pattern to find in the graph. RETURN then selects which parts of the match to include in the result set.

MATCH (c:Customer)
RETURN c.id
LIMIT 5
  • (c:Customer) binds each Customer node to the variable c.
  • c.id reads the id property.
  • LIMIT 5 caps the output at five rows.
# MATCH all Customer nodes and RETURN their names
result = conn.query("MATCH (c:Customer) RETURN c.id LIMIT 5")
result.show()
2

Counting Results

Aggregation with count()

count() is an aggregation function that returns the number of matched items. query_scalar() is a convenience method that returns a single value directly, perfect for queries that produce one row with one column.

# Count all Customer nodes using query_scalar()
total = conn.query_scalar("MATCH (c:Customer) RETURN count(c)")
print(f"Total customers: {total}")
3

Ordering Results

Sorting with ORDER BY

ORDER BY sorts results by one or more expressions. Add DESC for descending order. Combine with LIMIT to get “top N” results.

# Order customer names alphabetically
result = conn.query("""
MATCH (c:Customer)
RETURN c.id AS name
ORDER BY c.id
""")
result.show()
4

Displaying Results

Using result.show()

The show() method renders query results as a formatted table inside Jupyter. It auto-detects whether to display tabular data or a graph visualisation.

# Display a richer result set with show()
result = conn.query("""
MATCH (c:Customer)
RETURN c.id AS name, c.bk_sectr AS sector, c.acct_stus AS id_type
ORDER BY c.id
""")
result.show()

Key Takeaways

  • MATCH finds patterns in the graph; RETURN selects what to output
  • query_scalar() returns a single value (ideal for count())
  • ORDER BY sorts results; LIMIT restricts row count
  • result.show() renders a formatted table in Jupyter