Quick revision
The short answer
I decide by the query patterns, not by the shape of the data alone. Recursive CTEs handle trees and shallow graphs well, SQL/PGQ from SQL:2023 adds graph pattern queries inside the relational database where the engine supports it (support is still uneven, so check your version), and a graph database pays off only for deep, cyclic, many-hop traversals with path constraints at a scale where SQL becomes slow and hard to write. We start with SQL, prove the need with real queries and numbers, and if we adopt a graph database we keep the relational system as the source of truth and feed the graph through CDC.
What to remember
In this answer, we saw that graph-shaped data ranges from trees to deep, cyclic graphs, and that the right tool depends on the query patterns. Recursive CTEs handle trees and shallow graphs well, SQL/PGQ from SQL:2023 brings pattern queries into the relational database where supported, and a graph database helps with deep, many-hop, path-constrained traversals but adds a system to sync, operate and staff. We start with SQL, prove a need with real queries and measurements, and if we adopt a graph database we keep the relational system as the source of truth and feed the graph through CDC.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I decide by the query patterns, not by the shape of the data alone. Recursive CTEs handle trees and shallow graphs well, SQL/PGQ from SQL:2023 adds graph pattern queries inside the relational database where the engine supports it (support is still uneven, so check your version), and a graph database pays off only for deep, cyclic, many-hop traversals with path constraints at a scale where SQL becomes slow and hard to write. We start with SQL, prove the need with real queries and numbers, and if we adopt a graph database we keep the relational system as the source of truth and feed the graph through CDC.
Background to this question
Graph-shaped data is everywhere once we look for it: managers and reports, parts and assemblies, roles that inherit permissions, accounts linked by shared devices. Each time a team meets it, someone proposes a graph database, and each proposal is a decision that adds a system the company must sync, run and staff for years. A senior leader is asked this question because the right answer is a repeatable way of deciding, not a favourite technology. In this answer, we will separate trees from true graphs, see what recursive SQL handles well and where it breaks down, look at SQL/PGQ, weigh what a graph database really costs, and finish with a decision process and an integration pattern.
1Trees, Shallow Graphs and Deep Graphs
The word graph covers very different workloads. An org chart is a tree: each node has one parent, there are no cycles, and depth is perhaps 12. A permission model is usually a shallow graph: a user belongs to groups, groups to other groups, perhaps four hops. A fraud ring or a supply chain is a deep graph: many paths between any two nodes, cycles everywhere, and questions like 'find every account within six hops that shares a device or a card with this one, but only through links created in the last 30 days'.
The difference matters because the cost of traversal grows with the number of paths explored. A tree query explores each node once. A six-hop search in a graph where each node has 50 neighbours can touch 50 to the sixth power paths, about 15 billion, unless the engine prunes cleverly.
2What Recursive SQL Handles Well, and Where It Breaks
Recursive CTEs, the WITH RECURSIVE queries that repeat a join until no new rows appear, are excellent for trees and shallow graphs. With an index on the edge columns, each iteration is a set of index lookups, and the relational database gives us transactions, constraints and the operational tools we already have.
They break down in three ways on deep graphs. The first is speed: the recursion explores paths breadth-first without a smart traversal strategy, and every hop multiplies the working set. The second is cycles: we must carry a path array or use the CYCLE clause to avoid looping, which makes rows larger at each hop. The third is expressiveness: a constraint such as 'alternating card and device edges, each newer than the last' turns into a query few people can review.
So the honest statement is not 'SQL cannot do graphs'. It is that recursive SQL is fine until the number of hops, the branching and the path rules grow, and then it becomes slow and hard to maintain at the same time.
3SQL/PGQ: Graph Queries Inside the Relational Database
The SQL:2023 standard added a part called SQL/PGQ, property graph queries. It lets us declare a property graph over existing tables, saying which tables are vertices and which are edges, and then query it with a MATCH clause that describes patterns, much like Cypher. The data stays in ordinary tables with ordinary transactions.
This is attractive because it removes the sync problem entirely. But support across engines is still uneven. Oracle Database 23ai supports it, some analytical engines offer it through extensions, and PostgreSQL does not have it in a released core version as far as I know, though work on it has been proposed; check the version you run. Even where it exists, the speed of deep traversals depends on how the engine executes the pattern, which we must measure.
4What a Graph Database Really Costs
A native graph database such as Neo4j stores each node with direct references to its edges, so following an edge is a pointer hop rather than an index lookup. Its query languages, Cypher and the new ISO standard GQL, express variable-length paths and pattern constraints compactly. For a fraud team running six-hop pattern searches thousands of times a day, that can be the difference between seconds and milliseconds.
The costs are just as real. It is another system to operate, with its own backups, upgrades, monitoring and on-call skills. The data must be kept in sync with the system of record, which introduces lag and failure modes. The team must learn a new query language and data modelling style. And total cost includes licences or managed-service fees. A rough comparison helps: if the graph workload is one dashboard used by five analysts, these costs dominate; if it is a real-time fraud check on every payment, the benefit can justify them.
| Option | Good at | Costs and risks |
|---|---|---|
| Recursive CTEs | Trees, shallow graphs, few hops | Slow and hard to read for deep, cyclic paths |
| SQL/PGQ (SQL:2023) | Pattern queries on existing tables | Uneven engine support; check your version |
| Graph database | Deep, many-hop, path-constrained traversal | New system to sync, operate and staff |
5A Decision Process Based on Evidence
I would ask teams to bring three things: the five most important queries written in plain language, the expected data size and growth, and the latency and freshness they need. Then we try the queries in SQL on realistic data. If recursive CTEs answer them within budget, we stop there; if SQL/PGQ is available on our engine and clearer, we use it. Only when the measured SQL is too slow or too complex to maintain do we prototype a graph database on the same data and compare.
The final decision also weighs consistency needs (does the fraud check need to see an edge created a second ago?), team skills, and who will own the system on call. Writing these into a short decision record keeps the choice explainable when the next team asks the same question.
- Is the data a tree or a shallow graph?YesRecursive CTEs with indexed edgesNoNext question
- Does SQL on realistic data meet the latency goal?YesStay in SQL; consider SQL/PGQ if supportedNoNext question
- Is the workload important and lasting?YesPrototype a graph database and measureNoPrecompute or cache the answers
- Does the prototype win clearly?YesAdopt, fed by CDC from the source of truthNoStay in SQL and optimise
4 steps in order. Play them, or step through them one at a time.
6If We Adopt One: Source of Truth and CDC
When a graph database is justified, I keep the relational database as the source of truth for accounts, devices and their links, because that is where transactions, constraints and audits live. The graph becomes a read model. Change data capture, CDC, reads the relational database's write-ahead log (for example with Debezium into Kafka) and applies each insert, update and delete to the graph.
This design makes the graph rebuildable: if it is corrupted or its model changes, we replay from a snapshot plus the change stream. It also makes the lag explicit. We measure it, alert when it exceeds the agreed freshness, say 5 seconds for fraud checks, and the payment flow decides what to do when the graph is behind.
7Follow-up questions
How would you handle permissions, which are a graph but must be exact and current?
Permissions are usually shallow, so I would keep them in SQL with a recursive query over group membership, cache the expanded set per user with a short expiry, and invalidate on change. If the model is deep, a closure table of user to effective group, maintained transactionally, gives exact and current answers with simple joins. A separate graph database with replication lag is risky for authorisation decisions.
What if one team has already adopted a graph database without this process?
I would not force a rewrite. I would review it against the same questions: is it fed from a source of truth, is the lag measured, who is on call, and do its queries really need it. Often the outcome is to keep it but put it on the standard CDC pattern and document ownership, so it stops being a special case.
Is precomputation an option instead of traversal?
Often, yes. If the business question is 'which accounts are in the same cluster', a nightly or streaming job can compute connected components and store a cluster_id on each account. Reads then become a simple equality lookup. Traversal at query time is only needed when the question changes per request or must reflect edges created seconds ago.
8Conclusion
In this answer, we saw that graph-shaped data ranges from trees to deep, cyclic graphs, and that the right tool depends on the query patterns. Recursive CTEs handle trees and shallow graphs well, SQL/PGQ from SQL:2023 brings pattern queries into the relational database where supported, and a graph database helps with deep, many-hop, path-constrained traversals but adds a system to sync, operate and staff. We start with SQL, prove a need with real queries and measurements, and if we adopt a graph database we keep the relational system as the source of truth and feed the graph through CDC.
Open with
I'd start by separating the workloads, because graph covers very different things. Org charts are trees and permissions are usually shallow graphs, and recursive CTEs with indexed edge columns handle those well inside the database we already run.
Then, if they want more
The pain starts with deep, cyclic, many-hop traversals with path rules, like fraud rings six hops out, where recursive SQL explodes in work and becomes very hard to read.
SQL:2023 added SQL/PGQ, property graph queries with a MATCH syntax over existing tables, which would avoid a second system, but engine support is still uneven, so I'd check what our version offers. A graph database gives native traversal and Cypher or GQL, but it is another system to sync, operate and staff.
So my process is: teams bring their top queries, data volumes and latency needs, we try them in SQL on realistic data, and only if SQL clearly fails do we prototype a graph database and measure. I'd also consider precomputing answers like cluster ids.
If we adopt one, the relational database stays the source of truth and the graph is a read model fed by CDC, with measured lag and a way to rebuild it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Graph-shaped data (org charts, supply chains, permissions, fraud rings) keeps appearing...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/61f7df82ea/