
Master the Cypher Query Language: The Graph SQL
Learn the industry-standard language for graph retrieval. Master the syntax of MATCH, WHERE, and RETURN to find complex patterns and relationship chains across your knowledge base.
Master the Cypher Query Language: The Graph SQL
In the world of relational databases, we have SQL. In the world of Graph RAG, we have Cypher. Cypher is the language of "Pattern Matching." Instead of describing how to find the data (table joins, selects), you describe what the data looks like (ASCII art circles and arrows).
In this lesson, we will master the core syntax of Cypher. We will learn how to write nodes (), relationships [], and patterns ()->[]->(). We will explore the "Holy Trinity" of Cypher: MATCH, WHERE, and RETURN, and see why this visual language is so much better suited for AI-augmented retrieval than classic SQL.
1. The Art of the Pattern (ASCII Syntax)
Cypher is designed to look like the graph it is querying.
- A Node:
(p:Person {name: 'Sudeep'}) - A Relationship:
-[:WORKS_AT]-> - A Complete Fact:
(p:Person)-[:WORKS_AT]->(o:Office)
The Visualization: If you can draw your question on a whiteboard, you can write it in Cypher.
2. The Big 3: MATCH, WHERE, and RETURN
MATCH: The Pattern Finder
This is where you define the connectivity you are looking for.
MATCH (p:Person)-[:LEADS]->(project:Project)
WHERE: The Filter
This acts like the SQL WHERE clause, filtering based on properties or logic.
WHERE project.budget > 1000000 AND p.role = 'Lead'
RETURN: The Output
This defines what the AI agent actually receives as context.
RETURN p.name, project.title, project.status
3. Variable Length Relationships (The Multi-Hop)
This is the "Secret Weapon" of Graph RAG.
MATCH (p:Person)-[:WORKS_WITH*1..2]-(colleague)
The *1..2 means "Find anyone I work with DIRECTLY (1 hop) or INDIRECTLY (2 hops)." This single line of code replaces what would be multiple complex JOINs in SQL.
graph LR
subgraph "The Cypher Pattern"
A((p:Person)) -->|WORKS_AT| B((o:Office))
end
subgraph "The Result Set"
R1[Sudeep | London]
R2[Jane | Tokyo]
end
A -.->|Scan| R1
A -.->|Scan| R2
style A fill:#4285F4,color:#fff
style B fill:#34A853,color:#fff
4. Aggregation and Logic: WITH and COLLECT
Sometimes you don't want a list of rows; you want a "Context Block."
MATCH (p:Person)-[:HAS_SKILL]->(s:Skill)RETURN p.name, collect(s.name) as skills
The RAG Benefit: Instead of sending 10 different snippets to the LLM, you send one clean sentence: "Sudeep has the skills: [Python, Graphs, AI, Cloud]." This conserves tokens and improves clarity.
5. Implementation: Writing Your First Cypher Script
Let's look at a real-world pattern for a Graph RAG system.
// QUESTION: Find common projects between Sudeep and Jane
MATCH (s:Person {name: 'Sudeep'})-[:CONTRIBUTES_TO]->(p:Project)
MATCH (j:Person {name: 'Jane'})-[:CONTRIBUTES_TO]->(p)
WHERE p.status = 'Active'
RETURN p.title as CommonProject, p.lastUpdate as Date;
// LOGIC:
// The first MATCH finds Sudeep's projects.
// The second MATCH 'filters' that list to only ones Jane is also on.
// The WHERE ensures we don't return old, dead projects.
6. Summary and Exercises
Cypher is the "Translation Layer" between human questions and graph facts.
- Patterns are defined using ASCII art syntax:
(node)-[:REL]->(node). - MATCH is for finding shapes; WHERE is for filtering values.
- Variable Length queries enable easy multi-hop reasoning.
- COLLECT builds clean context arrays for the LLM.
Exercises
- Syntax Task: Write a Cypher query to find "The names of all projects that depend on a project named 'Apollo'."
- Filter Drill: How would you modify your query to only find projects with a
priorityproperty ofHigh? - Visualization: Can you draw the pattern
(a)-[:FOLLOWS]->(b)-[:FOLLOWS]->(a)? What does this represent in a social network?
In the next lesson, we will look at how to refine these queries for AI: Writing Pattern-Matching Queries for AI.