Lesson 2 of 2 Graph Data

Graph Queries with MATCH and SHORTEST_PATH

This lesson explains the MATCH pattern syntax and how SHORTEST_PATH and graph aggregate functions find and describe paths of variable length.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the arrow pattern syntax of MATCH.
  • Identify when to use SHORTEST_PATH and the FOR PATH and quantifier syntax.
  • Describe the graph path aggregate functions.

MATCH patterns

Node and edge tables are listed in the FROM clause with aliases, and MATCH in the WHERE clause describes how they connect with arrow syntax: node-(edge)->node or node<-(edge)-node. Several patterns are combined with AND, and a pattern can reuse a node alias to form a chain. MATCH cannot be combined with OR or NOT around the pattern, though other predicates can be added with AND.

-- Products bought by people that user 7 follows, with how many of them bought each one
SELECT p.Name, COUNT(DISTINCT friend.UserID) AS Buyers
FROM graph.[User] AS me, graph.Follows AS f, graph.[User] AS friend,
     graph.Bought AS b, graph.Product AS p
WHERE MATCH(me-(f)->friend-(b)->p)
  AND me.UserID = 7
GROUP BY p.Name
ORDER BY Buyers DESC;
True or false

MATCH patterns

Exam skills 1.3.6

Decide whether each statement about MATCH is true.

Log in to try this activity Interactive activities are free with an account.

SHORTEST_PATH and graph functions

SHORTEST_PATH finds the shortest path between nodes through repeated edges. The tables repeated in the path are marked FOR PATH, the repetition is written (-(edge)->node)+ for one or more hops or {1,3} for a bounded number, and aggregate functions with WITHIN GROUP (GRAPH PATH) describe the path, such as STRING_AGG of names, COUNT of hops and LAST_VALUE for the end node.

SELECT me.Name,
       STRING_AGG(other.Name, ' -> ') WITHIN GROUP (GRAPH PATH) AS Path,
       COUNT(other.UserID) WITHIN GROUP (GRAPH PATH) AS Hops,
       LAST_VALUE(other.Name) WITHIN GROUP (GRAPH PATH) AS Reaches
FROM graph.[User] AS me,
     graph.Follows FOR PATH AS f,
     graph.[User] FOR PATH AS other
WHERE MATCH(SHORTEST_PATH(me(-(f)->other){1,3}))
  AND me.UserID = 7;
Match the pairs

Describing a path

Exam skills 1.3.6

Match each part of a SHORTEST_PATH query with what it does.

Log in to try this activity Interactive activities are free with an account.

Combining graph and relational queries

Graph queries are ordinary T-SQL: they can join node tables to relational tables, filter with any predicate, group results and feed CTEs. A common pattern computes candidates with MATCH and then ranks them with window functions or vector similarity, for example recommending products bought by friends and ordering them by how similar their description embeddings are to the user's recent purchases.

Worked example

Degrees of separation

A professional network must show, for a given member, every member reachable through 'knows' relationships within three hops, with the chain of names. Which query construct should the developer use?

Use MATCH(SHORTEST_PATH(m(-(k)->other){1,3})) with the Knows edge table and the repeated member table marked FOR PATH, and STRING_AGG(other.Name, ' -> ') WITHIN GROUP (GRAPH PATH) to show the chain. A fixed three-hop MATCH pattern would return only members exactly three hops away and would need separate patterns for one and two hops, and a recursive CTE would work but needs manual cycle handling.

Check yourself

Which tables must be marked FOR PATH in a SHORTEST_PATH query?

Show answer

The node and edge tables that repeat inside the path pattern; the starting node is not marked.

Exam Tips

Remember for the exam

  • Traversal pattern: MATCH(a-(e)->b) in the WHERE clause, combined with AND.
  • Paths of variable length: SHORTEST_PATH with FOR PATH tables.
  • Bound the hops: the {1,3} quantifier.
  • Describe a path: STRING_AGG, COUNT or LAST_VALUE WITHIN GROUP (GRAPH PATH).