Labeled property graphs and RDF triple stores: Cypher traversals, fraud-ring patterns, and the LPG vs RDF paradigm choice.
⏱ 11 min readTopics chapter readerLevel · Specialized & Applied
01 · Overview
Start here
⏱ 6 min · Topic 1 of 9
Some questions are about links. Who bought what? Who brought in a new customer?
Which table feeds which table? That is a question about links too.
A graph stores data as things and the links between them. This chapter shows how to model a graph and how to ask it questions. It also shows when plain tables are still the better choice.
Recap: Entities, Relationships & Cardinality showed how things link. Keys & Identity gave each thing an id.
Normalization & Denormalization showed that a join rebuilds a link at query time. NoSQL & Access-Pattern Modeling named graph stores as one of four kinds of NoSQL store. This chapter covers that kind.
What you will learn
Nodes, relationships, labels and properties.
Cypher: how to ask a graph a question.
When a fact is a node, a relationship, or a property.
Walks of many hops, in Cypher and in a SQL recursive CTE.
When a graph helps, and when plain tables are enough.
02 · The model
The property graph model
⏱ 9 min · Topic 2 of 9
Most graph databases use the property graph model. You need four words:
A node is one thing, such as a customer or a product.
A label is the type of a node, such as Customer or Product.
A relationship, also called an edge, links two nodes. It has a type, such as BOUGHT, and a direction.
A property is a named value on a node or on a relationship, such as name or quantity.
Now draw the shared shop as a graph. Customers are nodes, products are nodes, and each row of order_items becomes a link. Here are the 10 nodes:
Nodes (10)
Label
Nodes
Customer
Asha, Ben, Carla, Dev, Omar
Product
Pen, Notebook, Bag, Lamp, Chair
And here are the 7 BOUGHT relationships. Each one points from a customer to a product. The order id and the quantity are properties of the link:
BOUGHT relationships (7)
From
Type
To
order_id
quantity
Asha
BOUGHT
Pen
101
2
Asha
BOUGHT
Notebook
101
1
Asha
BOUGHT
Bag
102
1
Ben
BOUGHT
Lamp
103
1
Carla
BOUGHT
Pen
104
3
Dev
BOUGHT
Chair
106
1
Dev
BOUGHT
Lamp
106
1
Omar is a node with no links, because he has no orders. Order 105 has no items, so it adds no link.
Cypher is the query language many graph databases use. This is how you create one customer, one product and one link:
Create two nodes and one relationshipworked example
Round brackets make a node. Asha has the label Customer and two properties. The arrow makes a link that points from Asha to Pen.
The direction is stored. Asha BOUGHT Pen does not say that Pen BOUGHT Asha.
Watch out
If your query uses the wrong arrow, it returns no rows and no error. MATCH (p:Product)-[:BOUGHT]->(c:Customer) finds nothing here. When a query is empty, check the arrow first.
A second graph model is RDF, the Resource Description Framework. It stores each fact as a triple: subject, predicate, object, such as Asha, bought, Pen.
A plain triple has no room for a property like quantity, so RDF needs an extra step for it. The rest of this chapter uses the property graph.
03 · Ask a graph
Ask questions with Cypher
⏱ 7 min · Topic 3 of 9
In Cypher you draw the shape you want to find. Nodes go in round brackets. A relationship goes in square brackets, with an arrow.
MATCH finds every place in the graph where your shape fits. RETURN says which values to show. The short names c, b and p let you use a part in RETURN.
The Pen buyers are Asha and Carla. Asha also bought Notebook and Bag. Carla bought only Pen, so she adds nothing.
In SQL the links live in a table. Here is bought, with one row for each customer and product. The same question needs a self-join, which joins a table to itself:
The answer is the same. For a fixed two hops, SQL is short enough. The self-join is the SQL form of the second hop.
GQL is the standard language for graph queries from ISO, the International Organization for Standardization. ISO published it in 2024 as ISO/IEC 39075. Its patterns look much like Cypher.
04 · Design
Node, edge or property
⏱ 6 min · Topic 4 of 9
For each fact, you choose: node, relationship, or property. Start from the questions, as in NoSQL & Access-Pattern Modeling. Nouns usually become nodes, and verbs become relationships.
Use this table as a guide:
Where does each fact go?
Fact
Choice
Why
Customer Asha
A Customer node.
It has its own id and its own links.
Asha bought a Pen
A BOUGHT relationship.
It links two nodes.
Quantity 2 on that purchase
A property of BOUGHT.
It describes the link. You never walk to a quantity.
Pen is in the category Stationery (made up)
A property, or a Category node.
A property if you only show it. A node if you walk by it.
Order 106
An Order node, or a property of BOUGHT.
A node when it joins three or more things.
A fact becomes a node when you want to walk to it, or when it joins three or more things. Order 106 joins Dev, Chair and Lamp.
Say you give each order its own node. The meaning of "bought together" changes. It now means in the same order, not by the same customer:
Products in the same order as Lampworked example
SQL
Input data
CONTAINS relationships (7), from order_items7 rows
Lamp is in orders 103 and 106. Order 103 holds only a Lamp. Order 106 also holds a Chair, so one row comes back.
One more habit helps. Give each meaning its own relationship type, such as BOUGHT or PLACED. One RELATED_TO type with a "kind" property makes every query check that property.
Watch out
Do not turn every property into a node. Each extra node adds a hop to every question that passes through it, and the graph grows for no gain.
05 · Many hops
Walk many hops
⏱ 10 min · Topic 5 of 9
A traversal is a walk along relationships. A variable-length path matches any number of hops in a range. In Cypher you write a star: *1..3 means one to three hops.
To try it, add made-up REFERRED links. Each link means one customer brought in another. They are not part of the shared shop:
REFERRED links (made up)
Referrer
Brought in
Asha (1)
Ben (2)
Asha (1)
Carla (3)
Carla (3)
Dev (4)
Dev (4)
Omar (5)
Who did Asha bring in, directly or through others? The star with no upper limit walks as far as the links go:
The variable p holds the whole path, and length(p) counts its links. Omar is three hops away: Asha to Carla to Dev to Omar.
In SQL, a fixed join chain needs one more join for each hop. A recursive CTE has no such limit. A CTE is a named query that starts with WITH.
CTEs, Subqueries & Query Structure in the SQL module teaches recursive CTEs. In short, a start query finds the first hop. A step query joins the rows found last round to the table.
The start query returns customers 2 and 3. The first repeat adds 4 at hop 2, and the next adds 5 at hop 3. Then no rows are new.
Watch out
If two customers referred each other, the SQL step would never stop. Add a limit, such as WHERE d.hops < 10, to the step query. Cypher never uses one relationship twice in a path, so it stops by itself.
Busy nodes
A busy node has a huge number of links. People call it a super-node.
Imagine a Pen bought by a million customers. A walk through it reaches a million nodes.
Four habits help:
Limit the number of hops.
Filter on a property of the link early, such as quantity.
Start from the small side of the question and walk toward the busy node.
Do not run an unbounded walk from a busy node.
06 · Choose
When to use a graph store
⏱ 7 min · Topic 6 of 9
Two things decide it: how many hops, and what you want back. Here are five kinds of question:
Which tool fits which question
Question
Example
Good fit
One or two fixed hops
Who bought a Pen? Pen buyers also bought what?
SQL joins.
Unknown depth
Everyone in Asha’s REFERRED chain.
A recursive CTE, or a graph query.
Totals over many rows
Units sold for each product.
SQL GROUP BY, in a warehouse.
Many hops with rules on the links
Accounts linked by shared cards or devices, such as a fraud ring.
A graph database.
The route is the answer
Which tables feed this report, step by step.
A graph database.
Most questions in the first three rows do not need a graph database. A graph database pays off when deep walks are your main work. That is when a recursive CTE gets hard to write or to keep fast.
A model or a database
A graph model is a way to think about your data. You can keep it in tables and walk it with a recursive CTE. A graph database stores nodes and relationships directly and has a graph query language.
Neo4j is a graph database that uses Cypher.
Amazon Neptune is a graph database that Amazon Web Services (AWS) runs for you. Its property graphs use openCypher, an open form of Cypher, or Gremlin, another graph language. Its RDF graphs use SPARQL, the query language for RDF.
A second database means a second copy of your data to keep in sync. Totals over many rows stay in the warehouse. Many teams use both.
Interview question: when use a graph database?
Say that fixed hops are joins, and a walk of unknown depth can be a recursive CTE. A graph database pays off when deep walks or whole paths are your main work. Add that totals stay in the warehouse.
07 · Recap
What you now know
⏱ 4 min · Topic 7 of 9
Five ideas to keep:
A property graph has labelled nodes and typed, directed relationships. Both can have properties.
The arrow in your query must match the stored direction, or you get no rows.
Make a fact a node when you walk to it or when it joins three or more things.
A star walks many hops. A recursive CTE does the same in SQL. Stop loops with a limit.
Use joins for fixed hops, a recursive CTE for an occasional deep walk, and a warehouse for totals. Use a graph database when deep walks are your main work.
Check yourself
In the shop graph, which way does BOUGHT point? What does the other arrow return?
How many hops is it from Asha to Omar in the REFERRED data?
Why does an Order node change what "bought together" means?
08 · Practice
Practice lab
⏱ 3 min · Topic 8 of 9
Each exercise opens a canvas for an entity-relationship diagram (ERD). You draw the model, and the review checks it against sample rows.
Start with the first one. Write down the questions first. Then decide which facts are nodes, links or properties.
Graphs model links inside one system. The next problem is across systems. The same customer can sit in the shop, the sales system and the support desk, with different ids and spellings.
⏱ 3 min · Topic 9 of 9
Next chapter
Master Data Management (MDM)
Graphs model links inside one system. The next problem is across systems. The same customer can sit in the shop, the sales system and the support desk, with different ids and spellings.
Chapter 18 covers Master Data Management. You build one trusted record for each customer from many systems that disagree.