Inferensys

Glossary

Cypher Query Language

Cypher is a declarative, ASCII-art inspired query language specifically designed for querying and manipulating property graph databases.
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.
QUERY LANGUAGE

What is Cypher Query Language?

Cypher is the declarative, pattern-matching query language for property graph databases, most notably Neo4j.

Cypher Query Language is a declarative, ASCII-art-based language specifically designed for querying and manipulating property graph data. It allows users to express complex graph patterns intuitively by describing the visual structure of nodes, relationships, and their properties they wish to find or create. Its syntax is built around the core concept of pattern matching, making it highly readable for developers familiar with graph concepts. Cypher is the native query language for the Neo4j graph database and has influenced the open openCypher project and the GQL international standard.

Cypher operates on a labeled property graph model, where nodes (entities) and relationships (connections) can have labels and key-value properties. A query describes a pattern, such as (p:Person)-[:LIVES_IN]->(c:City), which the database engine matches against the stored graph. It supports standard CRUD operations, aggregation, filtering, and pathfinding. For enterprise use, Cypher integrates with ACID transactions, index-free adjacency for fast traversals, and is a core component of Knowledge Graph as a Service platforms, enabling deterministic querying of interconnected business data.

QUERY LANGUAGE

Key Features of Cypher

Cypher is a declarative, ASCII-art-inspired query language for property graph databases. Its core strength lies in intuitively expressing graph patterns to find, create, update, and delete data.

01

Declarative Pattern Matching

Cypher is a declarative language, meaning you describe what data you want, not how to retrieve it. You define patterns using an intuitive ASCII-art syntax.

  • Example: (p:Person)-[:LIVES_IN]->(c:City) finds all Person nodes connected to City nodes via a LIVES_IN relationship.
  • The engine's query optimizer determines the most efficient execution plan, abstracting traversal logic from the developer.
02

Property Graph Model

Cypher is designed for the property graph model, where both nodes (entities) and relationships (connections) can have properties (key-value pairs).

  • Nodes are represented in parentheses: (:Person {name: 'Alice', age: 30}).
  • Relationships use brackets and arrows: -[:WORKS_AT {since: 2020}]->.
  • This model allows for a rich, schema-flexible representation of connected data, closely mirroring real-world domains.
03

Clauses for Data Manipulation

Cypher uses a structured set of clauses to compose queries, similar to SQL.

  • MATCH: Locates patterns in the graph.
  • WHERE: Filters results with predicates.
  • RETURN: Specifies what data to output.
  • CREATE / MERGE: Inserts new data or ensures it exists.
  • SET / REMOVE: Updates or deletes properties.
  • DELETE: Removes nodes and relationships.
  • WITH: Pipes results between query parts, enabling complex multi-step operations.
04

Path Queries & Variable-Length Traversals

A core capability is querying for paths—sequences of nodes and relationships. Cypher excels at variable-length traversals to discover connections of unknown depth.

  • Use * for variable hops: (:Person)-[:KNOWS*1..5]->(:Person) finds chains of KNOWS relationships between 1 and 5 hops long.
  • The shortestPath() function finds the optimal path between two nodes.
  • This is essential for recommendation systems, fraud detection (finding rings), and network analysis.
05

Aggregations & Graph Analytics

Cypher supports powerful aggregation functions and collection operations for analytical workloads directly on the graph structure.

  • Use COUNT(), SUM(), COLLECT() in a RETURN clause with GROUP BY.
  • Built-in graph algorithms like PageRank and Louvain community detection can be invoked via Cypher procedures (e.g., CALL gds.pageRank.stream).
  • This enables business intelligence and graph data science without exporting data to external systems.
06

Index-Free Adjacency & Performance

Cypher queries are optimized for index-free adjacency, a native graph storage paradigm where nodes hold direct pointers to connected relationships. This enables constant-time traversals regardless of total graph size.

  • Query planners use statistical metadata to select optimal start nodes and traversal directions.
  • Composite indexes and full-text indexes accelerate property lookups.
  • Query caching and parameterization ($param) improve throughput for repeated query patterns.
Cypher Query Language

How Cypher Works: Pattern Matching

Cypher is a declarative, ASCII-art-inspired query language designed specifically for property graph databases, allowing users to express complex graph patterns intuitively.

Cypher works by allowing users to describe graph patterns using a visual, ASCII-art syntax where parentheses () represent nodes, arrows --> or -- represent relationships, and square brackets [] can hold relationship details. The core operation is pattern matching, where the query engine searches the graph for all subgraphs that conform to the specified pattern. This declarative approach abstracts away the procedural steps of traversal, letting users focus on what data to find, not how to find it. For example, (p:Person)-[:LIVES_IN]->(c:City) matches all person nodes connected to city nodes via a LIVES_IN relationship.

The language's power comes from combining these patterns with clauses like MATCH (to find patterns), WHERE (to filter results), RETURN (to project data), and CREATE (to insert data). Its design enables index-free adjacency lookups, where traversing from one node to its connected nodes is a constant-time operation. This makes Cypher exceptionally efficient for exploring deep, connected data, forming the foundation for graph-based RAG and complex relationship discovery in enterprise knowledge graphs without requiring complex joins.

QUERY LANGUAGE COMPARISON

Cypher vs. SQL vs. SPARQL

A feature comparison of three declarative query languages designed for different underlying data models: property graphs, relational tables, and RDF graphs.

Feature / ParadigmCypherSQLSPARQL

Primary Data Model

Property Graph (Nodes, Relationships, Properties)

Relational (Tables, Rows, Columns)

RDF Graph (Subject-Predicate-Object Triples)

Core Query Construct

Graph Pattern Matching (ASCII-art syntax)

Table Selection and Joins (SELECT ... FROM ... WHERE)

Graph Pattern Matching (Triple pattern syntax)

Schema Flexibility

Schema-optional (labels & types provide structure)

Schema-required (strict table definitions)

Schema-flexible (ontology provides optional constraints)

Path Query Support

Native, declarative path patterns with variable length

Requires recursive common table expressions (CTEs)

Native via property paths in SPARQL 1.1

Aggregation & Grouping

Supported (e.g., COUNT, COLLECT, GROUP BY)

Extensive, mature support

Supported (e.g., COUNT, GROUP BY)

Inference & Reasoning

Limited to path expansion; no built-in logical reasoner

None (purely based on stored data)

Native via entailment regimes (e.g., OWL 2 RL, RDFS)

Federated Query Support

Limited; typically vendor-specific

Extensive (via database links, FDWs)

Native in SPARQL 1.1 (SERVICE clause)

Primary Use Case

Navigating connected data, fraud detection, recommendations

Transactional reporting, aggregating columnar data

Integrating heterogeneous data, semantic web, linked data

APPLICATION DOMAINS

Where is Cypher Used?

Cypher is the native query language for Neo4j and is supported by several other graph systems. Its primary application is querying and manipulating property graph data across diverse enterprise domains.

03

Graph Analytics & Business Intelligence

Cypher is used to power graph-based analytics, uncovering insights through relationship-centric queries that are cumbersome in SQL.

  • Fraud Detection: Find complex rings of connected entities (e.g., (p1:Person)-[:TRANSFERRED_TO*3..5]->(p2:Person)).
  • Recommendation Engines: Query for paths between users and products ("People who bought X also bought Y").
  • Supply Chain Analysis: Traverse supplier networks to identify single points of failure or optimize routes.
  • Social Network Analysis: Calculate centrality metrics (PageRank, betweenness) using built-in or user-defined procedures.
04

Knowledge Graph Querying & Reasoning

Within Enterprise Knowledge Graphs, Cypher is used to query interconnected entities and their properties, serving as a foundational layer for deterministic reasoning.

  • Semantic Search: Traverse ontology-based relationships to answer complex questions (e.g., "Which projects use deprecated libraries?").
  • Data Lineage & Provenance: Trace the origin and transformation of data assets through explicit relationship chains.
  • Graph-Based RAG: Retrieve precise subgraphs as context for LLMs, providing factual grounding and eliminating hallucinations. Cypher queries fetch connected facts, not just isolated chunks.
05

Application Backends & Microservices

Cypher is executed from within application code via official drivers, making it the interface between business logic and the graph data layer.

  • Official Drivers: Use Neo4j drivers for Java, JavaScript, Python, .NET, and Go to send parameterized Cypher queries.
  • Parameterized Queries: Prevent injection attacks and enable query reuse (e.g., MATCH (p:Person {name: $name})).
  • Transaction Management: Group multiple Cypher operations within ACID transactions to ensure data integrity.
  • Microservice Integration: A dedicated graph microservice often exposes endpoints that execute specific Cypher queries to serve graph data to other services.
06

Data Science & Machine Learning Workflows

Data scientists use Cypher to extract connected datasets for feature engineering and to operationalize graph-native ML models.

  • Feature Extraction: Query subgraphs around entities to create predictive features (e.g., a customer's network influence).
  • Graph Algorithm Execution: Call Graph Algorithm Library functions (like Louvain community detection) via Cypher procedures (CALL gds.louvain.stream).
  • Graph Embedding Generation: Use Cypher to project a graph and feed it into a Graph Neural Network (GNN) Service.
  • Link Prediction: Write Cypher queries to find candidate pairs for relationship prediction models.
CYPHER QUERY LANGUAGE

Frequently Asked Questions

Cypher is the declarative query language for property graph databases, designed for intuitive pattern matching. These FAQs address its core mechanics, use cases, and how it compares to other graph query languages.

Cypher is a declarative, pattern-matching query language specifically designed for querying and manipulating property graph databases. It works by allowing users to describe visual patterns of nodes and relationships using an ASCII-art syntax, which the database's query engine then matches against the stored graph structure. For example, the query MATCH (p:Person)-[:LIVES_IN]->(c:City) RETURN p.name, c.name finds all Person nodes connected to City nodes via a LIVES_IN relationship and returns their names. The engine optimizes and executes this pattern, traversing the graph's inherent index-free adjacency for high-performance lookups.

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.