Inferensys

Glossary

Cypher Query Language

Cypher is a declarative graph query language developed for Neo4j, designed for efficient querying and updating of property graphs using an ASCII-art syntax for pattern matching.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
GRAPH QUERY LANGUAGE

What is Cypher Query Language?

Cypher is a declarative, ASCII-art-inspired query language designed specifically for the property graph model, enabling intuitive and efficient querying and manipulation of graph data.

The Cypher Query Language is a declarative graph query language developed for Neo4j that uses an intuitive, ASCII-art syntax to express graph patterns for matching, creating, updating, and deleting nodes and relationships. Its core strength lies in its ability to visually map the structure of a query to the shape of the data in the graph, making complex traversals and pattern matching highly readable. Unlike imperative languages, Cypher describes what to retrieve, not how to retrieve it, allowing the underlying graph database engine to optimize execution.

Cypher operates on the property graph model, where entities are nodes with labels and key-value properties, connected by directed, typed relationships which can also hold properties. Its syntax centers on clauses like MATCH (for pattern matching), WHERE (for filtering), RETURN (for projecting results), and CREATE/MERGE (for writing data). As a domain-specific language, it is optimized for graph operations like variable-length path finding and recursive traversals, which are cumbersome in SQL. While initially proprietary to Neo4j, an open-source project, openCypher, aims to establish it as a standard, with implementations in other systems like Apache AGE.

GRAPH QUERY LANGUAGE

Key Features of Cypher

Cypher is a declarative graph query language developed for Neo4j, designed for efficient querying and updating of property graphs using an intuitive ASCII-art syntax for pattern matching.

01

ASCII-Art Pattern Matching

Cypher's most distinctive feature is its use of ASCII-art syntax to visually represent graph patterns. Nodes are depicted with parentheses (), relationships with arrows --> or --, and properties within curly braces {}. This makes complex graph traversals intuitive to write and read.

  • Example: MATCH (p:Person)-[:LIVES_IN]->(c:City {name: 'Berlin'}) RETURN p.name
  • The pattern (p:Person)-[:LIVES_IN]->(c:City...) directly mirrors the visual structure of the graph, connecting a Person node to a City node via a LIVES_IN relationship.
02

Declarative Syntax

Like SQL, Cypher is a declarative language. You specify what data to find (the pattern), not how to traverse the graph. The Cypher query planner and optimizer determine the most efficient execution path, leveraging indexes and graph statistics.

  • Key clauses follow a logical flow: MATCH (find patterns), WHERE (filter results), RETURN (project data).
  • This abstraction allows developers to focus on the data model and business logic, while the database engine handles performance optimization.
03

Property Graph Model Native

Cypher is natively designed for the property graph model, which consists of:

  • Nodes (vertices) with labels (e.g., :Person, :Product) and key-value properties.
  • Relationships (edges) with a type (e.g., :PURCHASED, :WORKS_FOR) and direction, which can also have properties.
  • This model is more flexible for many enterprise use cases than the RDF triple model, allowing attributes to be directly attached to both entities and connections.
04

Clauses for Reading & Writing

Cypher provides a comprehensive set of clauses for full CRUD (Create, Read, Update, Delete) operations on the graph.

  • Read Clauses: MATCH, OPTIONAL MATCH, WHERE, RETURN, ORDER BY, LIMIT.
  • Write Clauses: CREATE (nodes/relationships), MERGE (create or match), SET (update properties), DELETE/DETACH DELETE (remove elements), REMOVE (delete labels/properties).
  • Combining Operations: Read and write clauses can be combined in a single query, enabling complex transactional operations like finding a pattern and creating new connected data.
05

Path Finding & Variable-Length Relationships

Cypher excels at expressing complex traversals and pathfinding queries using a concise syntax for variable-length relationships.

  • Use * to match paths of arbitrary length: (:Person)-[:KNOWS*]->(:Person).
  • Specify a range: [:KNOWS*2..5] finds paths between 2 and 5 hops long.
  • The shortestPath() and allShortestPaths() functions provide optimized algorithms for common graph problems, directly within the query language.
06

Aggregation & List Comprehensions

Cypher supports powerful aggregation functions and operations on collections, similar to features in modern programming languages.

  • Aggregation: Functions like COUNT(), COLLECT(), SUM(), and AVG() work with GROUP BY semantics.
  • List & Map Projection: Create lists and maps directly in RETURN: RETURN p.name, [ (p)-[:HAS_SKILL]->(s) | s.name ] AS skills.
  • Pattern Comprehensions: A compact syntax for filtering and projecting within a sub-pattern, enabling complex nested queries without multiple MATCH clauses.
GRAPH QUERY LANGUAGES

Cypher vs. SPARQL: A Comparison

A technical comparison of the declarative query languages for property graphs (Cypher) and RDF graphs (SPARQL), highlighting their core paradigms, syntax, and ecosystem fit.

Feature / MetricCypher (Neo4j)SPARQL (W3C Standard)

Primary Data Model

Property Graph (Nodes, Relationships, Properties)

RDF Graph (Subject-Predicate-Object Triples)

Core Query Paradigm

ASCII-art pattern matching (e.g., (a)-[:KNOWS]->(b))

Graph pattern matching via triple patterns (e.g., ?a :knows ?b)

Schema & Validation

Optional, user-defined via constraints & indexes. No formal semantics.

Defined via RDFS or OWL ontologies. SHACL for shape-based validation.

Built-in Reasoning

Standardization Body

OpenCypher (de facto), ISO/GQL (in progress)

World Wide Web Consortium (W3C) Recommendation

Primary Use Case

Interactive querying & analytics on connected, attributed data.

Semantic web integration, linked data querying, & ontology-based reasoning.

Path Query Syntax

Variable-length relationships: (a)-[:KNOWS*1..5]->(b)

Property Paths: ?a :knows+ ?b

Federated Query Support

Via APOC procedures or Neo4j Fabric (vendor-specific).

Native SERVICE keyword for querying remote SPARQL endpoints.

Update Operations

CREATE, MERGE, SET, DELETE, REMOVE

INSERT, DELETE, LOAD, CLEAR (SPARQL Update)

Result Transformation

RETURN clause for projecting results. WITH for piping.

SELECT (tabular), CONSTRUCT (new RDF graph), DESCRIBE (resource description).

PATTERN MATCHING

Common Cypher Query Examples

Cypher is a declarative graph query language for Neo4j, using an intuitive ASCII-art syntax to find patterns in property graphs. Below are foundational and practical query examples.

01

Find Nodes by Label

The most basic query retrieves all nodes of a specific type (label). Use MATCH with parentheses and a colon.

Example: MATCH (p:Person) RETURN p

  • Returns all nodes labeled Person.
  • The RETURN clause specifies what data to output.
  • You can limit results: MATCH (p:Person) RETURN p LIMIT 10.
02

Traverse Relationships

Cypher excels at finding connected data. Use arrows (--> or <--) to traverse relationships, specifying their type in square brackets.

Example: MATCH (p:Person)-[:WORKS_FOR]->(c:Company) RETURN p.name, c.name

  • Finds all Person nodes connected to a Company node via a WORKS_FOR relationship.
  • The direction of the arrow matters; -[:WORKS_FOR]-> means the relationship goes from Person to Company.
  • Returns the names of the people and their employers.
03

Filter with WHERE

The WHERE clause filters results based on node/relationship properties, similar to SQL.

Example: MATCH (p:Person) WHERE p.age > 30 AND p.name STARTS WITH 'A' RETURN p

  • Filters Person nodes where the age property is greater than 30 and the name property starts with 'A'.
  • Can use operators: =, <>, <, >, CONTAINS, ENDS WITH.
  • WHERE is often used with pattern matching: MATCH (p)-[:LIKES]->(m:Movie) WHERE m.year = 2023 RETURN p.name.
04

Create Nodes and Relationships

The CREATE clause inserts new graph elements. Use parentheses for nodes and brackets for relationships.

Example: CREATE (alice:Person {name: 'Alice', age: 30}), (bob:Person {name: 'Bob'}), (alice)-[:KNOWS {since: 2020}]->(bob)

  • Creates two Person nodes with property sets ({...}).
  • Creates a KNOWS relationship from alice to bob with a since property.
  • Tip: Use MERGE instead of CREATE to avoid duplicates, as it checks for existence first.
05

Variable-Length Paths

Find paths of uncertain depth using the * operator. Essential for hierarchical or network data.

Examples:

  • MATCH (p:Person)-[:FRIEND_OF*1..3]->(fof:Person) RETURN p, fof
    • Finds friends-of-friends, traversing 1 to 3 FRIEND_OF hops.
  • MATCH path = (c:Company)<-[:WORKS_FOR*]-(e:Employee)
    • Finds all employees connected to a company at any depth (unbounded).
    • The path variable captures the entire sequence of nodes and relationships.
06

Aggregate and Group Data

Use aggregation functions (COUNT, SUM, AVG, COLLECT) with RETURN and GROUP BY logic.

Example: MATCH (p:Person)-[:REVIEWED]->(m:Movie) RETURN m.title, AVG(p.rating) AS avg_rating, COUNT(*) AS reviews ORDER BY avg_rating DESC

  • For each movie, calculates the average rating and counts the number of reviews.
  • Groups results implicitly by the non-aggregated column (m.title).
  • ORDER BY sorts the final results. COLLECT(p.name) would create a list of reviewer names.
CYPHER QUERY LANGUAGE

Frequently Asked Questions

Cypher is the declarative graph query language developed for Neo4j. It uses an intuitive ASCII-art syntax for pattern matching, making it the primary tool for interacting with property graph databases.

Cypher is a declarative, ASCII-art-inspired query language specifically designed for querying and updating property graph databases, most notably Neo4j. It allows users to express complex graph patterns in a human-readable syntax that visually resembles the structure of the graph itself. Unlike imperative languages, you describe what data you want to find or manipulate, not how to traverse the graph, leaving query optimization to the database engine. Its core strength lies in its intuitive pattern matching for nodes, relationships, and their properties, making it the standard language for developers working with labeled property graphs.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.