Path Patterns and Quantifiers

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

A MATCH path is a walk through your graph. Such as customer, then order, then product. This page shows how to follow more than one hop, choose a direction, and repeat a hop.

The examples use the commerce graph from Defining a Property Graph.

Follow More Than One Hop 

Chain the walk in the order the objects interact with each other. Such as how a shopper’s order actually happens.

This pattern finds two different customers who ordered the same product.

1MATCH (i1:individual)-[:customerOrder]->(:salesOrder)-[:orderProduct]->(product:product)
2    <-[:orderProduct]-(:salesOrder)<-[:customerOrder]-(i2:individual)
3WHERE i1.name != i2.name

Read it as: start at one customer, go to their order, go to the product, then walk back out through another order to another customer.

You can leave out a vertex or edge at either end. Data 360 SQL fills in an anonymous element.

Written patternSame as
MATCH -[e:customerOrder]->(order:salesOrder)MATCH ()-[e:customerOrder]->(order:salesOrder)
MATCH (i:individual)-[e:customerOrder]->MATCH (i:individual)-[e:customerOrder]->()
MATCH -[:orderProduct]->(product:product)<-[:orderProduct]-MATCH ()-[:orderProduct]->(product:product)<-[:orderProduct]-()

Separate independent patterns with commas. If two patterns reuse the same variable name, Data 360 SQL fuses them into one connected pattern. That’s how (item:product), (item:featured) keeps only products that are also featured.

Choose a Direction 

The arrow says which way to traverse the edge between two objects.

PatternDirectionMeaning
(a)-[e]->(b)RightWalk from a to b.
(a)<-[e]-(b)LeftWalk from b to a.
(a)-[e]-(b)EitherWalk either way. A one-way edge that isn’t a self-loop can match twice, once in each writing.
1-- Orders Avery placed
2MATCH (i:individual)-[:customerOrder]->(order:salesOrder) WHERE i.name = 'Avery'
3
4-- The customer for order O-100
5MATCH (order:salesOrder)<-[:customerOrder]-(i:individual) WHERE order.orderId = 'O-100'
6
7-- A customer and an order connected in either writing
8MATCH (i:individual)-[:customerOrder]-(order:salesOrder)

Repeat a Hop 

A quantifier repeats an edge a set number of times. Put it on the edge, not on the vertex.

QuantifierMeaning
{n}Exactly n hops.
{m,n}Between m and n hops, inclusive.

A quantifier works on a right, left, or either-way edge.

In the commerce graph, one hop is not enough to reach a product. Customer to order is one hop. Order to product is the second hop.

1-- Avery's products: customer -> order -> product
2MATCH (i:individual)-[]-{2}(product:product)
3
4-- Avery's products, plus products reached through another customer's order
5MATCH (i:individual)-[]-{2,4}(product:product)

Hops in the middle aren’t limited to a label.

These patterns omit the edge label because the two hops use different labels: customerOrder, then orderProduct. An unlabeled edge can match either one.

You can filter every repeated hop.

1MATCH (i:individual)-[e WHERE e.createdDate IS NOT NULL]-{2}(:product)

A variable on a quantified edge is a group variable. The local WHERE runs on every hop, which is why e.createdDate IS NOT NULL can require a date on both the order hop and the line-item hop. Don’t use that variable in the outer WHERE or COLUMNS.

These forms aren’t supported yet: {0,2}, {2,}, *, +, ?, and shortest-path queries.

Example: Products Connected to Avery 

A two-hop path is Avery’s own product. A four-hop path can reach a product through another customer who ordered something Avery also ordered. The ending :product label drops paths that stop on an order or another customer.

1WITH pg AS PROPERTY GRAPH (...)
2SELECT * FROM GRAPH_TABLE(pg
3    MATCH (i:individual)-[]-{2,4}(product:product)
4    WHERE i.name = 'Avery'
5    COLUMNS (
6        i.name AS individual_name,
7        product.productId AS product_id,
8        product.name AS product_name
9    )
10) AS gt;

The query returns one row for each path. The same product can appear more than once if more than one path reaches it.

This query returns other customers connected to Avery by exactly four hops: Avery → order → shared product → other order → other customer.

1WITH pg AS PROPERTY GRAPH (...)
2SELECT * FROM GRAPH_TABLE(pg
3    MATCH (i:individual)-[]-{4}(other:individual)
4    WHERE i.name = 'Avery'
5    COLUMNS (
6        i.individualId AS individual_id,
7        other.individualId AS connected_individual_id,
8        other.name AS connected_individual_name
9    )
10) AS gt;

Related Documentation