October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Cypher Cheat Sheet: Common Neo4j Queries and Examples

Quickly recognize and compose common Neo4j Cypher queries, from graph reads and optional matches to writes, deletes, and plan inspection.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Cypher queries describe graph patterns and what to do with the nodes, relationships, and paths they find. Use this cheat sheet for common Neo4j read, write, update, delete, and tuning patterns; check your server’s Neo4j version before relying on version-specific syntax.

How to read Cypher patterns

Cypher is Neo4j’s declarative graph query language: you describe a pattern, then specify operations on the matched data. Parentheses represent nodes; square brackets represent relationships. Labels and relationship types make patterns more specific. Keywords are not case-sensitive, but variable names are case-sensitive. The current Cypher manual is the reference for syntax and behavior.

As an Amazon Associate I earn from qualifying purchases.

Find graph data with MATCH

MATCH requires the specified pattern to be present. This example finds movies acted in by a person whose name is supplied as a parameter:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
MATCH (p:Person {name: $name})-[:ACTED_IN]->(m:Movie)
RETURN m.title AS title
ORDER BY title

$name is a parameter rather than a value embedded in the query. RETURN chooses the output column, and ORDER BY sorts the results. See the manual’s MATCH clause reference.

Match an optional pattern

Use OPTIONAL MATCH when an earlier match is required but a later pattern may be absent. Any missing optional portion is returned as null.

MATCH (p:Person {name: $name})
OPTIONAL MATCH (p)-[r:DIRECTED]->(movie)
RETURN p.name, r, movie

This query returns the person if found, even if they have no outgoing DIRECTED relationship. Put WHERE alongside the clause whose pattern it filters: it is a subclause of MATCH, OPTIONAL MATCH, or WITH, not a standalone filter in those contexts. See OPTIONAL MATCH and WHERE.

Pass, aggregate, and filter results with WITH

WITH forms a pipeline boundary: it can aggregate, calculate, rename, sort, or filter values before the next clause. Only variables named in the projection continue into the next stage, unless you use WITH *.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
MATCH (c:Customer)-[:BUYS]->(p:Product)
WITH c, count(p) AS purchases
WHERE purchases > 2
RETURN c.name, purchases
ORDER BY purchases DESC

Here, the query counts matched products for each customer, keeps customers with more than two purchases, then returns names and counts. Variables omitted from WITH leave scope, subject to the manual’s rules for subqueries. See WITH.

Create new data or match-or-create with MERGE

CREATE always creates the specified pattern

Use CREATE when each execution should add the specified graph pattern.

CREATE (p:Person {name: $name})
RETURN p

Running this again creates another matching node; it does not first check whether one already exists. See CREATE.

MERGE matches or creates the stated pattern

MERGE matches the whole pattern you specify, or creates it if no match exists. Choose that pattern deliberately: the identity implied by the pattern determines what is considered a match.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
MERGE (p:Person {email: $email})
ON CREATE SET p.createdAt = datetime()
ON MATCH SET p.lastSeen = datetime()
RETURN p

The conditional clauses set a property on creation or on a match. MERGE by itself should not be treated as a guarantee of uniqueness in every concurrency or schema situation. See MERGE.

Turn a list into rows with UNWIND

UNWIND expands a list into rows. It is often useful for processing parameterized batches, as in this example:

UNWIND $rows AS row
MERGE (p:Person {id: row.id})
SET p.name = row.name
RETURN count(p) AS processed

Supply $rows as a list of records with the expected fields. Validate inputs and choose a transaction strategy appropriate to the data volume; for production-scale imports, consult Neo4j’s operations documentation. See also the UNWIND reference.

Delete nodes and relationships carefully

DELETE removes a relationship or a node without relationships. To remove a node and its connected relationships, use DETACH DELETE:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
MATCH (p:Person {id: $id})
DETACH DELETE p

A query such as MATCH (n) DETACH DELETE n removes all graph data; run it only when that is explicitly intended. Neo4j’s cheat sheet also describes transactional batching for large deletion jobs and notes that this does not remove indexes or schema. See DELETE and the Cypher cheat sheet.

Shape and combine query results

Return only the needed output

RETURN determines the result columns. Keep it focused on the properties or values the application needs, rather than returning every node and relationship by default. To page through results, use ORDER BY with SKIP and LIMIT; pagination needs a deliberate sort if results must have a stable order. The RETURN reference covers these options.

Combine result sets with UNION

Use UNION to combine compatible result sets while removing duplicate rows; use UNION ALL to preserve duplicates. The component queries must return the same number of columns, with corresponding columns compatible in type. See UNION.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Inspect indexes and query plans

Neo4j’s cheat sheet lists range (the default), text, point, and token lookup indexes for search performance, and shows full-text and vector index syntax. An index may help a particular workload, but it does not guarantee a speedup; measure the query and data you actually use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • EXPLAIN inspects the planned query without executing it.
  • PROFILE executes the query and reports runtime operators and measurements.

Use these tools to investigate how a query runs, then consult the manual’s planning and tuning guidance before changing indexes or query structure.

Practical habits for safer, clearer queries

  • Parameterize values such as names, IDs, and batches instead of building query text from user input.
  • Bound variable-length patterns when the traversal depth should be limited; unbounded traversals can match far more paths than expected.
  • Return only the fields needed by the caller.
  • Use EXPLAIN or PROFILE to inspect plans rather than assuming an index or syntax change will be faster.
  • For writes, decide whether the operation should always create (CREATE) or match-or-create the stated pattern (MERGE).

Check Cypher version compatibility

Available syntax depends on the Neo4j release. The current manual documents version prefixes including CYPHER 25 and CYPHER 5. It states that on Neo4j 2025.06 or later, CYPHER 25 selects Cypher 25 supported by the running server; CYPHER 5 selects Cypher 5 as it existed at the Neo4j 2025.06 release. Verify your deployed server’s version and consult its matching manual, especially before using newer syntax such as FILTER, dynamic labels or types, or WHEN. See the Cypher manual.

Continue learning Cypher

Neo4j’s GraphAcademy lists a free Cypher Fundamentals course covering graph reads and writes. Its course catalog also includes intermediate topics such as filtering, variable-length traversal, WITH, subqueries, UNWIND, and parameters.

For a book-length treatment, Neo4j’s recommended-books page lists Graph Data Processing with Cypher by Ravindranatha Anthapu, published by Packt, as a practical guide to building graph traversal queries with Cypher on Neo4j. Check that page for current edition and availability details: Neo4j recommended books.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.