Skip to content

Relational and graph in one query

View as Markdown

CoraDB is one database engine for relational and graph data, not two engines side by side. A graph is stored in vertex tables (nodes) and edge tables (relationships), and these are tables like any other. That means:

  • Vertex and edge tables can be read and changed with ordinary SQL;
  • A MATCH graph pattern is just another source in the FROM clause of a SELECT, so one statement can traverse the graph and join relational tables, then filter, group, sort and aggregate the result;
  • Inserts and updates of rows, nodes and edges take part in the same transaction.

The examples below run on the e-commerce sample database: orders and order_items are relational tables, customer, product, category, brand and city are vertex tables, and likes, follows, part_of and resides_in are edge tables. The relational tables refer to vertices through ordinary foreign keys (orders.customer_id → customer.id, order_items.product_id → product.id).

A vertex table can be queried without any graph syntax. The products that cost more than 100:

SELECT name, unit_price, units_in_stock
FROM product
WHERE unit_price > 100
ORDER BY unit_price DESC
LIMIT 3;
name unit_price units_in_stock
Côte de Blaye 263.50 17
Thüringer Rostbratwurst 123.79 0

It also joins relational tables like any table. The customers with the most orders (customer is a vertex table, orders a relational table):

SELECT c.company_name AS customer, COUNT(*) AS orders
FROM orders o
JOIN customer c ON c.id = o.customer_id
GROUP BY c.id, c.company_name
ORDER BY orders DESC, customer
LIMIT 3;
customer orders
Mayer, Carroll and Owens 40
Young and Sons 40
Kelly Ltd 39

A MATCH pattern can be listed in the FROM clause together with relational tables and joined to them in WHERE. The best-selling categories: the graph says which category a product belongs to (part_of edge), the relational order_items table says how many units were sold:

SELECT cat.name AS category, SUM(oi.quantity) AS units_sold
FROM MATCH (p:product)-[:part_of]->(cat:category),
order_items oi
WHERE oi.product_id = p.id
GROUP BY cat.name
ORDER BY units_sold DESC
LIMIT 5;
category units_sold
Shorts 6528
Tees 6232
Erin Recommends 5843
Hoodies & Sweatshirts 5779
Jackets 5394

The pattern can also be the right side of a JOIN. The cities with the most orders: orders is joined to the customers of the resides_in pattern, which gives each customer's city:

SELECT ci.name AS city, COUNT(*) AS orders, SUM(o.shipping_cost) AS shipping
FROM orders o
JOIN MATCH (c:customer)-[:resides_in]->(ci:city) ON c.id = o.customer_id
GROUP BY ci.name
ORDER BY orders DESC
LIMIT 5;
city orders shipping
Portland 113 1924.59
San Francisco 103 1804.23
Elgin 102 1760.51
London 102 1576.63
Kirkland 101 1797.71

Edges often record behaviour (a customer likes a product, follows a brand) while relational tables record transactions. One query can compare them. The customers who bought the most of the products they like:

SELECT c.company_name AS customer, COUNT(DISTINCT p.id) AS liked_and_bought
FROM MATCH (c:customer)-[:likes]->(p:product),
orders o, order_items oi
WHERE o.customer_id = c.id
AND oi.order_id = o.id
AND oi.product_id = p.id
GROUP BY c.company_name
ORDER BY liked_and_bought DESC, customer
LIMIT 3;
customer liked_and_bought
Harris, Hernandez and Hancock 3
Moore-Anderson 3
Simpson-Atkins 3

Relational results as input for a graph query

Section titled “Relational results as input for a graph query”

A common table expression can compute a relational result first and feed it to a pattern. The Nike followers who order the most:

WITH spend AS (
SELECT customer_id, COUNT(*) AS orders, SUM(shipping_cost) AS shipping
FROM orders
GROUP BY customer_id
)
SELECT c.company_name AS customer, s.orders, s.shipping
FROM MATCH (c:customer)-[:follows]->(b:brand),
spend s
WHERE b.name = 'Nike'
AND s.customer_id = c.id
ORDER BY s.orders DESC, customer
LIMIT 5;
customer orders shipping
Mayer, Carroll and Owens 40 653.14
Kelly Ltd 39 638.48
Smith-Williams 25 442.54
Murphy, Nelson and Donovan 20 292.04
Murray Ltd 20 383.64

Changes to relational tables, vertex tables and edge tables are ordinary DML in the same transaction: they are committed or rolled back together. An order, its order line and a new likes edge:

INSERT INTO orders (id, customer_id, order_date, shipping_cost, ship_address)
VALUES (90001, 1, DATE '2026-11-19', 4.90, '12 Main Street, Portland');
INSERT INTO order_items (order_id, product_id, quantity, discount_pct)
VALUES (90001, 5, 1, 0);
INSERT EDGE FROM (SELECT c FROM customer c WHERE c.id = 1)
TO (SELECT p FROM product p WHERE p.id = 5)
INTO likes VALUES (DATETIME '2026-11-19 10:00:00');
COMMIT;

Before the COMMIT, the same session already sees all three changes, in one query:

SELECT c.company_name AS customer, p.name AS product, l.since, oi.quantity
FROM MATCH (c:customer)-[l:likes]->(p:product),
order_items oi
WHERE c.id = 1 AND p.id = 5
AND oi.order_id = 90001 AND oi.product_id = p.id;
customer product l.since quantity
Woodward, Brown and Morton Chef Antons Gumbo Mix 2026-11-19 10:00:00.000 1

A ROLLBACK instead of the COMMIT removes the order, the order line and the edge together.