To achieve this using SQL Graph in Azure SQL, you use the MATCH clause to define the path. Since you need exactly two directed relationships (A →B →C), you must chain the edge table twice in the same direction. Transact-SQL Statement SELECT Person3.ID, Person3.NameColumn -- Replace with your actual nvarchar(100) column name FROM PersonNodeTable AS Person1, KnowsEdgeTable AS Knows1, PersonNodeTable AS Person2, KnowsEdgeTable AS Knows2, PersonNodeTable AS Person3 WHERE MATCH(Person1-(Knows1)->Person2-(Knows2)->Person3) AND Person1.ID = @InputID; Use code with caution. Key Components MATCH Clause: Defines the traversal pattern. The syntax (Node)-(Edge)->(Node) ensures the relationship is directed. Chaining: To get "exactly two" steps, you define three node aliases and two edge aliases. Aliases: Each instance of the table must have a unique alias (e.g., Person1, Person2, Person3) so the engine can distinguish between the different points in the path. Filtering: The WHERE clause uses your input parameter (@InputID) to set the starting point of the graph traversal Incorrect: [Not A] Need three persons, not two. [Not B] Do not use JOIN. [Not C] Incorrect MATCH statement. Reference: https://learn.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-sample
To achieve this using SQL Graph in Azure SQL, you use the MATCH clause to define the path.
Since you need exactly two directed relationships (A →B →C), you must chain the edge table twice in the same direction.
Transact-SQL Statement
SELECT
Person3.ID,
Person3.NameColumn -- Replace with your actual nvarchar(100) column name
FROM
PersonNodeTable AS Person1,
KnowsEdgeTable AS Knows1,
PersonNodeTable AS Person2,
KnowsEdgeTable AS Knows2,
PersonNodeTable AS Person3
WHERE
MATCH(Person1-(Knows1)->Person2-(Knows2)->Person3)
AND Person1.ID = @InputID;
Use code with caution.
Key Components
MATCH Clause: Defines the traversal pattern. The syntax (Node)-(Edge)->(Node) ensures the relationship is directed.
Chaining: To get "exactly two" steps, you define three node aliases and two edge aliases.
Aliases: Each instance of the table must have a unique alias (e.g., Person1, Person2, Person3) so the engine can distinguish between the different points in the path.
Filtering: The WHERE clause uses your input parameter (@InputID) to set the starting point of the graph traversal
Incorrect:
[Not A]
Need three persons, not two.
[Not B]
Do not use JOIN.
[Not C]
Incorrect MATCH statement.
Reference:
https://learn.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-sample