<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv" -->

---
title: Writing Cypher Queries That Don’t Lose Your Data | daily.dev
description: A hands-on walkthrough of a common Cypher pitfall in Neo4j: writing a MATCH query as one long chain of required relationships silently excludes nodes that...
canonical: https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Writing Cypher Queries That Don’t Lose Your Data | daily.dev
og:description: A hands-on walkthrough of a common Cypher pitfall in Neo4j: writing a MATCH query as one long chain of required relationships silently excludes nodes that...
og:url: https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv
og:image: https://api.daily.dev/og/posts/0Q6CY0JYV.png
og:image:alt: Writing Cypher Queries That Don’t Lose Your Data
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing Cypher Queries That Don’t Lose Your Data

**[neo4j](https://daily.dev/sources/neo4j)** · 8 min read · 0 upvotes · 0 comments

## Summary

A hands-on walkthrough of a common Cypher pitfall in Neo4j: writing a MATCH query as one long chain of required relationships silently excludes nodes that don't have those relationships (like a brand-new user with no enrolments). The fix is to anchor the query on the entity itself and pull related data via list comprehensions or COUNT{}/COLLECT{} subqueries, which return empty rather than filtering the row out entirely. The piece also explains why MERGE property placement affects duplication, and why OPTIONAL MATCH chains cause row multiplication and wasted database work compared to list comprehensions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/neo4j/writing-cypher-queries-that-dont-lose-your-data-074999e0d502>

## Questions this post answers

### Why does my Cypher MATCH query hide nodes that have no relationships, like a user with no enrolments?

A chained MATCH pattern requires every node to satisfy every hop in the pattern, so a node lacking any of those relationships is filtered out entirely rather than returned with empty values. The fix is to anchor the query on the entity itself, then fetch related data through a list comprehension or a COUNT{} or COLLECT{} subquery, which can return empty without removing the row.

_Developers debugging missing rows in graph query results can find similar Cypher patterns explained on daily.dev._

### Why does using multiple OPTIONAL MATCH clauses in Cypher cause duplicated rows?

Each OPTIONAL MATCH runs against every row produced by the prior clause, so if a user has three enrolments and two team memberships, the two OPTIONAL MATCH clauses combine to produce six duplicated rows instead of one. List comprehensions avoid this because each pattern expands separately and returns its own list, so results never multiply and the database does less work.

_Anyone tuning slow Cypher queries can track performance patterns like this via daily.dev._

### Why should MERGE in Cypher only include identity properties like email, with other fields set separately using SET?

MERGE only creates a new node when it fails to find an exact match for every property inside its brackets, so if a mutable property like a display name is included in the brackets, a later run with a corrected value creates a duplicate node instead of updating the existing one. Keeping only immutable identity properties in MERGE and setting everything else with SET prevents this duplication.

_Developers writing idempotent Cypher seed scripts can follow patterns like this through daily.dev._

## Similar posts on daily.dev

- [Pattern Matching: Turning Connected Data Into Actionable Intelligence](https://daily.dev/posts/pattern-matching-turning-connected-data-into-actionable-intelligence-wnapd4cm1) · neo4j · 1 upvotes · 0 comments
- [Graph-Style Query vs. Join: When Your REST API Hits a Wall](https://daily.dev/posts/graph-style-query-vs-join-when-your-rest-api-hits-a-wall-e15hkvvoq) · Atomic Spin · 0 upvotes · 0 comments
- [How to Build a Knowledge Graph with Python and Neo4j \[Full Handbook\]](https://daily.dev/posts/how-to-build-a-knowledge-graph-with-python-and-neo4j-full-handbook--drqcmnstt) · freeCodeCamp · 1 upvotes · 0 comments
- [Why basic RAG fails at multi-hop reasoning \(and how GraphRAG fixes it\)](https://daily.dev/posts/why-basic-rag-fails-at-multi-hop-reasoning-and-how-graphrag-fixes-it--yfmxhy36v) · The New Stack · 0 upvotes · 0 comments

---

Tags: [#architecture](https://daily.dev/tags/architecture), [#backend](https://daily.dev/tags/backend), [#neo4j](https://daily.dev/tags/neo4j)

[View this post on daily.dev](https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Writing Cypher Queries That Don’t Lose Your Data","url":"https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv"},"datePublished":"2026-09-07T19:30:07.937Z","dateModified":"2026-09-14T08:40:09.637Z","description":"A hands-on walkthrough of a common Cypher pitfall in Neo4j: writing a MATCH query as one long chain of required relationships silently excludes nodes that...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1775b5ea5ba756941532c2e3e1f796e8?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1775b5ea5ba756941532c2e3e1f796e8?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"neo4j","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"neo4j","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/a8f15865d92d4c789a415f316dcb81d2","url":"https://daily.dev/sources/neo4j"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"architecture,backend,neo4j","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"neo4j","item":"https://daily.dev/sources/neo4j"},{"@type":"ListItem","position":3,"name":"Writing Cypher Queries That Don’t Lose Your Data"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/writing-cypher-queries-that-don-t-lose-your-data-0q6cy0jyv#faq","mainEntity":[{"@type":"Question","name":"Why does my Cypher MATCH query hide nodes that have no relationships, like a user with no enrolments?","acceptedAnswer":{"@type":"Answer","text":"A chained MATCH pattern requires every node to satisfy every hop in the pattern, so a node lacking any of those relationships is filtered out entirely rather than returned with empty values. The fix is to anchor the query on the entity itself, then fetch related data through a list comprehension or a COUNT{} or COLLECT{} subquery, which can return empty without removing the row. Developers debugging missing rows in graph query results can find similar Cypher patterns explained on daily.dev."}},{"@type":"Question","name":"Why does using multiple OPTIONAL MATCH clauses in Cypher cause duplicated rows?","acceptedAnswer":{"@type":"Answer","text":"Each OPTIONAL MATCH runs against every row produced by the prior clause, so if a user has three enrolments and two team memberships, the two OPTIONAL MATCH clauses combine to produce six duplicated rows instead of one. List comprehensions avoid this because each pattern expands separately and returns its own list, so results never multiply and the database does less work. Anyone tuning slow Cypher queries can track performance patterns like this via daily.dev."}},{"@type":"Question","name":"Why should MERGE in Cypher only include identity properties like email, with other fields set separately using SET?","acceptedAnswer":{"@type":"Answer","text":"MERGE only creates a new node when it fails to find an exact match for every property inside its brackets, so if a mutable property like a display name is included in the brackets, a later run with a corrected value creates a duplicate node instead of updating the existing one. Keeping only immutable identity properties in MERGE and setting everything else with SET prevents this duplication. Developers writing idempotent Cypher seed scripts can follow patterns like this through daily.dev."}}]}
```

