Contents
Neo4j: Why Relationships Matter More Than the Data Itself

Neo4j: A World Where Relationships Take Center Stage

Most of us who work with data grew up on relational databases (SQL). Tables, rows, columns and JOINs are our daily bread. But what if there is a world where this model falls short and relationships between data matter more than the data itself? Welcome to graph databases and their king, Neo4j.

The problem: relational databases and complex relationships

Imagine building a social network. You need to answer an apparently simple question: “Show me the friends of my friends who live in Bratislava.”

In SQL it might look like this:

sql
SELECT p3.*
FROM People p1
JOIN Friends f1 ON p1.id = f1.person1_id
JOIN People p2 ON f1.person2_id = p2.id
JOIN Friends f2 ON p2.id = f2.person1_id
JOIN People p3 ON f2.person2_id = p3.id
WHERE p1.name = 'Ondrej'
  AND p3.city = 'Bratislava'
  AND p3.id != p1.id; -- We don't want to show ourselves

That is only for “friends of friends”. What if we wanted “friends of friends of friends”? The number of JOIN operations would grow, and the query could become slow and difficult to read. Relational databases are not designed for traversing deep, complex relationships in the same way.

The solution: a graph model and Neo4j

Neo4j approaches the problem differently. Instead of tables, it uses a simple but powerful model:

1

Nodes:

Represent entities such as Person, City or Company. They can have properties such as name and age.
2

typed

Represent connections between nodes. They are and , for example FRIENDS_WITH, LIVES_IN or WORKS_FOR. Relationships can also have properties, such as since: 2020.

What does our social network look like in Neo4j?

Instead of tables, imagine:

  • Node (p1:Person {name: 'Ondrej'})
  • Node (p2:Person {name: 'Jana'})
  • Relationship (p1)-[:FRIENDS_WITH]->(p2)

It is intuitive and looks much like a sketch on a whiteboard.

Cypher: a language that understands relationships

Neo4j has its own query language, Cypher, designed to be readable and to resemble a visual graph.

Let's revisit our question: “Show me the friends of my friends who live in Bratislava.”

cypher
MATCH (me:Person {name: 'Ondrej'})-[:FRIENDS_WITH*2]->(friend_of_friend:Person)
WHERE friend_of_friend.city = 'Bratislava'
RETURN friend_of_friend.name

Breaking down the Cypher query:

  • MATCH specifies the pattern to find in the graph.
  • (me:Person {name: 'Ondrej'}) finds a Person node named Ondrej and assigns it to me.
  • -[:FRIENDS_WITH*2]-> follows a FRIENDS_WITH relationship to a depth of exactly 2: a friend of a friend. For arbitrary depth, use *.
  • (friend_of_friend:Person) assigns the resulting node to friend_of_friend.
  • WHERE filters the results.
  • RETURN returns the names of the people found.

The query is shorter, easier to read and can be significantly faster for this type of operation because Neo4j is optimized for traversing relationships, an approach known as “index-free adjacency”.

When should you use Neo4j? Typical use cases

Graph databases do not replace every other database, but excel in specific domains:

  1. Social networks: community analysis, friend recommendations and influencer marketing.

  2. Recommendation engines: “Customers who bought this also bought...” is a classic graph problem.

    cypher
    MATCH (customer:Customer {id: 'user123'})-[:BOUGHT]->(p:Product)<-[:BOUGHT]-(other:Customer)-[:BOUGHT]->(recommended:Product)
    WHERE NOT (customer)-[:BOUGHT]->(recommended)
    RETURN recommended, count(*) AS frequency
    ORDER BY frequency DESC
    LIMIT 10
  3. Fraud detection: finding suspicious patterns, such as multiple supposedly different accounts using the same credit card or IP address.

    cypher
    MATCH (c1:Customer)-[:USED]->(card:CreditCard)<-[:USED]-(c2:Customer)
    WHERE c1 <> c2
    RETURN c1, c2, card
  4. Network and IT operations: mapping dependencies and analyzing the impact of a component outage.

  5. Knowledge graphs: organizing complex data, such as connections between medicines, diseases, proteins and symptoms. These can provide a foundation for advanced RAG systems.

The power of Neo4j and AI: GraphRAG

One modern application of Neo4j is GraphRAG (Graph Retrieval-Augmented Generation). Instead of searching unstructured text as in traditional RAG, an LLM can query a knowledge graph in Neo4j.

Workflow:

  1. A user asks: “Which employees worked on project 'X' and have experience with Python?”
  2. The LLM translates the question into a Cypher query.
  3. The query runs against a Neo4j database containing employees, projects and skills.
  4. Neo4j returns precise, structured data.
  5. The LLM formulates an answer based on that data.

The result can be more accurate and less prone to hallucinations, because the answer is grounded in structured facts from the graph.

Conclusion

Neo4j and graph databases are more than buzzwords. They are powerful tools for problems that may be too complex or slow in the relational world. If relationships, connections and networks are central to your domain, consider giving graphs a chance. Looking at data through relationships may reveal entirely new dimensions.

All articles