---
title: "Graph functions"
description: "CoraDB graph functions: nodes(), relationships(), path_size(), path_comp(), LABEL() and the JSON graph functions, with examples."
url: "https://www.coradb.ai/manual/sql/function/graph_fn.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/graph_fn.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb graph functions, nodes, relationships, path_size, path_comp, path comprehension, label, json_graph, json_info, json_node_properties, json_edge_properties, json_adjacency_list"
---

# Graph functions

Graph functions work on the nodes, edges and paths matched by a [MATCH](https://www.coradb.ai/manual/graph/match.html) pattern. Path functions take a **path variable** (`pth` in `MATCH pth = (a)-[...]-(b)`); element functions take a node or edge alias.

| Function | Returns |
|---|---|
| [nodes(path)](https://www.coradb.ai/manual/sql/function/graph_fn.html#nodes) | LIST of the vertices of a path, in order |
| [relationships(path)](https://www.coradb.ai/manual/sql/function/graph_fn.html#relationships) | LIST of the edges of a path, in order |
| [path_size(path)](https://www.coradb.ai/manual/sql/function/graph_fn.html#path_size) | Number of edges (hops) in a path |
| [path_comp(pattern ...)](https://www.coradb.ai/manual/sql/function/graph_fn.html#path_comp) | LIST built from every match of an inline pattern |
| [LABEL(element)](https://www.coradb.ai/manual/sql/function/graph_fn.html#label) | Name of the vertex or edge table of a node or edge |
| [JSON_NODE_PROPERTIES(node)](https://www.coradb.ai/manual/sql/function/graph_fn.html#json_node_properties) / [JSON_EDGE_PROPERTIES(edge)](https://www.coradb.ai/manual/sql/function/graph_fn.html#json_edge_properties) | JSON with the element id and its columns |
| [JSON_INFO(element)](https://www.coradb.ai/manual/sql/function/graph_fn.html#json_info) | JSON with id, label, columns and adjacency list |
| [JSON_ADJACENCY_LIST(node)](https://www.coradb.ai/manual/sql/function/graph_fn.html#json_adjacency_list) | JSON array of the node's edges |
| [JSON_GRAPH(elements...)](https://www.coradb.ai/manual/sql/function/graph_fn.html#json_graph) | JSON `{nodes, edges}` subgraph built from all matched rows (aggregate) |

They are typically combined with the list functions [first()](https://www.coradb.ai/manual/sql/function/list_fn.html#first), [last()](https://www.coradb.ai/manual/sql/function/list_fn.html#last), [at()](https://www.coradb.ai/manual/sql/function/list_fn.html#fn-at), [size()](https://www.coradb.ai/manual/sql/function/list_fn.html#size) and [list_comp()](https://www.coradb.ai/manual/sql/function/list_fn.html#list_comp), and with [property_of()](https://www.coradb.ai/manual/sql/function/information_fn.html#fn-property-of), which reads a column of a node or edge taken from a list:

```sql
property_of(element, column_name, vertex_or_edge_table)
```

The examples use the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html).

## NODES

```sql
nodes(path_variable)
```

Returns the vertices of a path as a LIST, from the first node to the last. The argument must be a path variable; a node alias, a scalar or NULL raises an error.

**Return type:** LIST of vertices

Products liked by a customer who also likes *Vegie-spread*. The path is product → customer → product, so position 0 is the start product, position 1 the customer and the last position the other product:

```sql
SELECT property_of(first(nodes(pth)), name, product) AS product,
       property_of(at(nodes(pth), 1), company_name, customer) AS liked_by,
       property_of(last(nodes(pth)), name, product) AS also_liked
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
ORDER BY 2, 3
LIMIT 5;
```

| product | liked_by | also_liked |
|---|---|---|
| Vegie-spread | Alvarado LLC | Lono Yoga Short |
| Vegie-spread | Alvarado LLC | Neve Studio Dance Jacket |
| Vegie-spread | Alvarado LLC | Zing Jump Rope |
| Vegie-spread | Boyd Inc | Helena Hooded Fleece |
| Vegie-spread | Brown Ltd | Balboa Persistence Tee |

> **Note:**
> `property_of()` must be given the table of the element at that position. In a path that mixes vertex tables, read each position separately instead of applying one table to every node with `list_comp()`.


## RELATIONSHIPS

```sql
relationships(path_variable)
```

Returns the edges of a path as a LIST, in path order. The argument must be a path variable.

**Return type:** LIST of edges

When were the two likes that link *Vegie-spread* to each co-liked product made?

```sql
SELECT p2.name AS other_product,
       property_of(first(relationships(pth)), since, likes) AS first_like_at,
       property_of(last(relationships(pth)), since, likes) AS second_like_at
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
ORDER BY 1
LIMIT 3;
```

| other_product | first_like_at | second_like_at |
|---|---|---|
| Abominable Hoodie | 2023-08-21 19:51:12.000 | 2025-03-29 22:27:02.000 |
| Aeon Capri | 2023-06-06 01:11:39.000 | 2024-08-03 17:54:24.000 |
| Arcadio Gym Short | 2022-11-04 14:49:12.000 | 2023-12-06 02:07:47.000 |

Use `ALL(... IN relationships(pth) WHERE ...)` / `NONE(...)` to test every edge of a path. Products co-liked with *Vegie-spread* where both likes date from 2022 or later:

```sql
SELECT COUNT(DISTINCT p2.id) AS products
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
  AND ALL(r IN relationships(pth) WHERE property_of(r, since, likes) >= DATETIME'2022-01-01 00:00:00');
```

| products |
|---:|
| 38 |


## PATH_SIZE

```sql
path_size(path_variable)
```

Returns the number of edges in the path, the same value as `size(relationships(path))`. The number of nodes is one more.

**Return type:** INT

```sql
SELECT path_size(pth) AS path_size,
       size(nodes(pth)) AS nodes,
       size(relationships(pth)) AS edges
FROM MATCH pth = ANY_SHORTEST (c1:customer)-[f:follows{1,6}]-(c2:customer)
WHERE c1.id = 5 AND c2.id = 15;
```

| path_size | nodes | edges |
|---:|---:|---:|
| 6 | 7 | 6 |

## PATH_COMP

```sql
path_comp(<graph_pattern> [, <graph_pattern> ...] [WHERE <condition>] | <expression> [ORDER BY <ordering>])
```

*Path comprehension*: matches the inline pattern, evaluates *expression* for every match and returns the values as one LIST. No `FROM MATCH` is needed.

- *graph_pattern*: one or more patterns, written as in `MATCH`. A single node without an edge is not a valid pattern.
- *condition*: optional filter on the matches.
- *expression*: required; the value collected for each match.
- *ordering*: optional order of the list (`ORDER BY 1` sorts by the expression).

**Return type:** LIST

All products liked by the customer *Hampton PLC*, alphabetically, in one value:

```sql
SELECT path_comp((cu:customer)-[l:likes]->(p:product)
                 WHERE cu.company_name = 'Hampton PLC'
                 | p.name ORDER BY 1) AS liked_products;
```

| liked_products |
|---|
| {'Compete Track Tote', 'Daria Bikram Pant', 'Diana Tights', 'Hollister Backyard Sweatshirt', 'Nora Practice Tank'} |

## LABEL

```sql
LABEL(node_or_edge)
```

Returns the name of the vertex table of a node, or of the edge table of an edge. Useful with edge alternatives such as `[r:likes|follows]` or with `UNION ALL` across relationship types.

**Return type:** STRING

```sql
SELECT LABEL(cu) AS node_label, LABEL(r) AS edge_label, ci.name
FROM MATCH (cu:customer)-[r:resides_in]->(ci:city)
WHERE cu.id = 5;
```

| node_label | edge_label | ci.name |
|---|---|---|
| customer | resides_in | Luleå |

## JSON graph functions

These functions return JSON values, convenient for applications and for graph visualisation tools. Every node and edge is identified by its internal object id, written `"(volume|page|slot)"`; the id is stable for the life of the row but is not the `id` column.


### JSON_NODE_PROPERTIES, JSON_EDGE_PROPERTIES

```sql
JSON_NODE_PROPERTIES(node)
JSON_EDGE_PROPERTIES(edge)
```

Return `{"id": ..., "properties": {column: value, ...}}` for one node or edge.

```sql
SELECT JSON_NODE_PROPERTIES(b) AS brand_props,
       JSON_EDGE_PROPERTIES(f) AS follow_props
FROM MATCH (cu:customer)-[f:follows]->(b:brand)
WHERE cu.id = 5;
```

```json
-- brand_props
{"id":"(0|4545|57)","properties":{"id":57,"name":"Skechers"}}
-- follow_props
{"id":"(0|4993|5)","properties":{"since":"07:02:33.000 PM 06/03/2025"}}
```


### JSON_ADJACENCY_LIST

```sql
JSON_ADJACENCY_LIST(node)
```

Returns a JSON array with one entry per edge of the node: `edge_id`, the id of the node at the other end (`dest`) and the direction (`dir`: `"0"` outgoing, `"1"` incoming).

```sql
SELECT JSON_ADJACENCY_LIST(b) AS neighbourhood
FROM MATCH (b:brand)
WHERE b.name = 'Lululemon';
```

```json
[{"edge_id":"(0|4801|8)","dest":"(0|4161|8)","dir":"0"},
 {"edge_id":"(0|4929|152)","dest":"(0|4481|132)","dir":"0"},
 ...
 {"edge_id":"(0|4993|23)","dest":"(0|4289|15)","dir":"1"},
 ...]
```

Lululemon has outgoing `located_at` and `manufactures` edges (`dir` 0) and incoming `follows` edges from customers (`dir` 1).


### JSON_INFO

```sql
JSON_INFO(node_or_edge)
```

Returns everything about an element in one document: `id`, `properties`, `label` and `adj_list` (the adjacency list).

```sql
SELECT JSON_INFO(s) AS supplier_info
FROM MATCH (s:supplier)
WHERE s.company_name = 'Exotic Liquids';
```

```json
{"id":"(0|4609|1)",
 "properties":{"id":1,"phone":"(171) 555-2222","address":"49 Gilbert St.",
               "contact_title":"Purchasing Manager","contact_name":"Charlotte Cooper",
               "company_name":"Exotic Liquids"},
 "label":"supplier",
 "adj_list":[{"edge_id":"(0|4737|1)","dest":"(0|4161|1)","dir":"0"}, ...]}
```


### JSON_GRAPH

```sql
JSON_GRAPH(element [, element ...])
```

An **aggregate** function: collects the listed nodes and edges of all matched rows into one subgraph, `{"nodes": [...], "edges": [...]}`, without duplicates. Each node has `id`, `label` and `properties`; each edge has `id`, `startNodeId`, `endNodeId`, `label` and `properties`. The format can be fed directly to graph visualisation libraries.

The customer *Hampton PLC*, the products it likes and the `likes` edges:

```sql
SELECT JSON_GRAPH(cu, l, p) AS subgraph
FROM MATCH (cu:customer)-[l:likes]->(p:product)
WHERE cu.company_name = 'Hampton PLC';
```

```json
{"nodes":[
   {"id":"(0|4289|66)","label":"customer","properties":{"id":65,"company_name":"Hampton PLC", ...}},
   {"id":"(0|4486|171)","label":"product","properties":{"id":1379,"name":"Hollister Backyard Sweatshirt","unit_price":52.0, ...}},
   ...],
 "edges":[
   {"id":"(0|5633|111)","startNodeId":"(0|4289|66)","endNodeId":"(0|4492|83)","label":"likes",
    "properties":{"since":"12:50:57.000 AM 09/28/2023"}},
   ...]}
```

## See also

- [MATCH clause: graph queries in SELECT](https://www.coradb.ai/manual/graph/match.html)
- [Collection functions](https://www.coradb.ai/manual/sql/function/list_fn.html)
- [JSON functions](https://www.coradb.ai/manual/sql/function/json_fn.html)
- [Sample database: e-commerce](https://www.coradb.ai/manual/sample-database.html)
