>_TheQuery
← Glossary

Cypher

Language, Vision & Retrieval

A declarative graph query language created for Neo4j that uses ASCII-art syntax to represent and match graph patterns.

Consider SQL but for relationships - instead of querying tables, you query patterns of connections between things.

Cypher is a declarative query language for property graphs, most closely associated with Neo4j. Instead of describing a sequence of database operations, you describe the nodes, relationships, and properties you want to match. Its ASCII-art syntax makes a graph pattern visible in the query: (alice:Person)-[:WORKS_FOR]->(company:Company) means that an Alice node is connected to a Company node by a WORKS_FOR relationship.

The core pattern

A Cypher query commonly uses MATCH to find a pattern and RETURN to choose what comes back. For example:

MATCH (person:Person)-[:WORKS_FOR]->(company:Company)
WHERE company.name = $company_name
RETURN person.name AS employee
ORDER BY employee

The parameter $company_name should be supplied separately by the application. Parameterization avoids turning user input into query syntax and also lets the database reuse query plans. Labels such as Person and Company describe node types; relationship types such as WORKS_FOR describe the edges between them. Properties live inside braces, for example (p:Person {name: $name}).

Reading and writing graphs

MATCH, OPTIONAL MATCH, WHERE, WITH, RETURN, and ORDER BY are common read-side clauses. CREATE adds new nodes or relationships. MERGE matches an existing pattern or creates it when it is absent, which is useful for idempotent imports but must be paired with appropriate constraints. SET updates properties, while DELETE and DETACH DELETE remove data. A production application should expose only the operations a user is authorized to perform; a generated query should never receive unrestricted write access by default.

Cypher can also traverse paths of different lengths, aggregate results with functions such as count() and collect(), and pass intermediate rows between stages with WITH. Indexes and constraints help the database find starting nodes efficiently, but a broad variable-length traversal can still become expensive. Query plans and realistic graph sizes matter more than the visual brevity of a query.

Cypher compared with other query languages

SQL is organized around tables, rows, and joins; Cypher is organized around connected patterns. SPARQL is designed for RDF triples and semantic-web vocabularies, while Cypher is commonly used with labeled property graphs. The languages can express related questions, but their data models, reasoning features, and operational tooling differ. Choosing one depends on how the graph is stored and what kind of traversal or semantic inference the application needs.

Cypher in RAG and agent systems

A text-to-Cypher system can translate a natural-language question into a graph query, then use the returned facts as context for an LLM. This is useful when the answer depends on explicit relationships, provenance, or multi-hop traversal. It also creates a security boundary: generated Cypher must be checked against an allowed schema, parameterized, limited by timeouts and row counts, and tested for destructive clauses. A safer design separates read-only query generation from any write-capable tool and returns source node identifiers or provenance alongside the answer.

Cypher is therefore more than a visual alternative to SQL. Its main advantage is expressing connected structure directly, while its production risks are the same ones found in any query language: unbounded work, unsafe writes, missing indexes, and trusting generated queries without validation.

Last updated: August 13, 2026