Monday, 3 August 2026

SPARQL Property Paths: Finding All Possible Paths Between Two Nodes

  

One of the most powerful features of SPARQL is its ability to traverse RDF graphs using property paths. Property paths allow us to navigate relationships that span multiple hops, making it possible to answer questions such as:

 

·      How are two people connected?

·      What route exists between two entities?

·      Which relationships participate in a connection between two nodes?

·      What dependencies link one asset to another?

 

As you progress on your journey toward becoming a SPARQL expert, you'll discover that property paths are among the most valuable tools available for graph analysis.

 

In this post, we'll tackle an advanced challenge, given two nodes in an RDF graph, can we identify all the triples that participate in paths connecting them?

 

We'll use a simple social network to demonstrate the technique.

 

socialNetwork.turtle

@prefix : <http://example.org/social/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

#################################################
# People
#################################################

:Ram rdf:type :Person ;
     :name "Ram" .

:Hari rdf:type :Person ;
      :name "Hari" .

:Krishna rdf:type :Person ;
         :name "Krishna" .

:Rahim rdf:type :Person ;
       :name "Rahim" .

:Priya rdf:type :Person ;
       :name "Priya" .

:John rdf:type :Person ;
      :name "John" .

:Anita rdf:type :Person ;
       :name "Anita" .

:Vikram rdf:type :Person ;
        :name "Vikram" .

:Neha rdf:type :Person ;
      :name "Neha" .

:Suresh rdf:type :Person ;
        :name "Suresh" .

:Meera rdf:type :Person ;
       :name "Meera" .

:David rdf:type :Person ;
       :name "David" .

:Alex rdf:type :Person ;
      :name "Alex" .

#################################################
# Friendship Network
#################################################

:Ram :knows :Hari .

:Hari :knows :Krishna .

:Krishna :knows :Rahim .

#################################################
# Branch 1
#################################################

:Priya :knows :Hari .

:John :knows :Priya .

#################################################
# Branch 2
#################################################

:Anita :knows :Krishna .

:Vikram :knows :Anita .

:Neha :knows :Vikram .

#################################################
# Branch 3
#################################################

:Suresh :knows :Hari .

:Meera :knows :Suresh .

#################################################
# Another route to Rahim
#################################################

:David :knows :Rahim .

#################################################
# Small cycle
#################################################

:Alex :knows :John .

:John :knows :Alex .

   

1. The Problem

Suppose we want to answer the following question "What paths exist between Ram and Rahim?".

 

At first glance, the answer seems obvious Ram → Hari → Krishna → Rahim.

 

However, rather than simply checking whether a path exists, we want to identify the individual triples that participate in the connection.

 

This is where SPARQL property paths shine.

 

Query

 

PREFIX : <http://example.org/social/>

SELECT ?subject ?predicate ?object
WHERE {

    :Ram (:|!:)* ?subject .

    ?subject ?predicate ?object .

    ?object (:|!:)* :Rahim .
}

Output

subject

predicate  

object

<http://example.org/social/Ram>

<http://example.org/social/knows>  

<http://example.org/social/Hari>

<http://example.org/social/Hari>   

<http://example.org/social/knows>  

<http://example.org/social/Krishna>

<http://example.org/social/Krishna>

<http://example.org/social/knows>  

<http://example.org/social/Rahim>

 

:Ram (:|!:)* ?subject .

This finds every node reachable from Ram. The expression (:|!:)* means:

 

·      Follow any predicate

·      Zero or more times

·      Only in the forward direction

 

?subject ?predicate ?object .

This captures an individual triple from the graph.

 

?object (:|!:)* :Rahim .

This verifies that the object can eventually reach Rahim. Together, these conditions identify triples that belong to at least one path connecting Ram and Rahim.

 

2. Making the Query Reusable

Hardcoding nodes is useful for demonstrations but not ideal in real applications. Instead, we can parameterize the query using VALUES.

 

PREFIX : <http://example.org/social/>

SELECT ?subject ?predicate ?object
WHERE {
  
    VALUES ?start { :Ram } 
    VALUES ?end { :Rahim }

    ?start (:|!:)* ?subject .

    ?subject ?predicate ?object .

    ?object (:|!:)* ?end .
}

   

Output

subject

predicate  

object

<http://example.org/social/Ram>

<http://example.org/social/knows>  

<http://example.org/social/Hari>

<http://example.org/social/Hari>   

<http://example.org/social/knows>  

<http://example.org/social/Krishna>

<http://example.org/social/Krishna>

<http://example.org/social/knows>  

<http://example.org/social/Rahim>

 

Just update the start variable to :John, to get the paths from John to Rahim.

 

PREFIX : <http://example.org/social/>

SELECT ?subject ?predicate ?object
WHERE {
  
    VALUES ?start { :John } 
    VALUES ?end { :Rahim }

    ?start (:|!:)* ?subject .

    ?subject ?predicate ?object .

    ?object (:|!:)* ?end .
}

   

In summary, Property paths are one of the most powerful capabilities offered by SPARQL. By combining property path expressions with ordinary triple patterns, we can identify the individual triples that participate in routes between two nodes in a graph.

 

In this post, we used a social network to:

 

·      Traverse arbitrary-length paths

·      Restrict traversal to a forward direction

·      Discover connections between people

·      Parameterize queries using VALUES

·      Handle cycles safely

·      Decompose graph routes into their constituent triples

 

This pattern is extremely useful whenever you need to understand how entities are connected inside an RDF graph. Whether you're working with social networks, data lineage platforms, organizational hierarchies, or knowledge graphs, property paths provide a powerful mechanism for exploring relationships that span multiple hops.


  

Previous                                                    Next                                                    Home

SPARQL Recursive Paths: Finding Friends of Friends with Property Paths

  

One of the most powerful features of SPARQL is its support for property paths, which allow us to traverse relationships across multiple hops in a graph.

 

In traditional SQL databases, finding indirect relationships often requires multiple self-joins. In SPARQL, however, a single property path expression can traverse a graph recursively, making it easy to answer questions such as:

 

·      Who knows Krishna directly or indirectly?

·      Who can eventually reach Krishna through a chain of friendships?

·      Which people are connected through friends of friends?

·      How can we navigate hierarchical or network structures without knowing the path length in advance?

 

In this post, we'll explore recursive paths using a simple social network example.

 

Consider the following RDF graph:

 

socialNetwork.turtle

@prefix : <http://example.org/social/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

#################################################
# People
#################################################

:Ram rdf:type :Person ;
     :name "Ram" .

:Hari rdf:type :Person ;
      :name "Hari" .

:Krishna rdf:type :Person ;
         :name "Krishna" .

:Rahim rdf:type :Person ;
       :name "Rahim" .

:Priya rdf:type :Person ;
       :name "Priya" .

:John rdf:type :Person ;
      :name "John" .

:Anita rdf:type :Person ;
       :name "Anita" .

:Vikram rdf:type :Person ;
        :name "Vikram" .

:Neha rdf:type :Person ;
      :name "Neha" .

:Suresh rdf:type :Person ;
        :name "Suresh" .

:Meera rdf:type :Person ;
       :name "Meera" .

:David rdf:type :Person ;
       :name "David" .

:Alex rdf:type :Person ;
      :name "Alex" .

#################################################
# Friendship Network
#################################################

:Ram :knows :Hari .

:Hari :knows :Krishna .

:Krishna :knows :Rahim .

#################################################
# Branch 1
#################################################

:Priya :knows :Hari .

:John :knows :Priya .

#################################################
# Branch 2
#################################################

:Anita :knows :Krishna .

:Vikram :knows :Anita .

:Neha :knows :Vikram .

#################################################
# Branch 3
#################################################

:Suresh :knows :Hari .

:Meera :knows :Suresh .

#################################################
# Another route to Rahim
#################################################

:David :knows :Rahim .

#################################################
# Small cycle
#################################################

:Alex :knows :John .

:John :knows :Alex .

   

1. Finding Direct Connections

Let's start with a simple query. Suppose we want to find people who directly know Rahim.

 

PREFIX : <http://example.org/social/>

SELECT ?personName
WHERE {
   ?person :knows :Rahim ;
           :name ?personName.
}

   

Output

 

personName
David
Krishna

   

Only Krishna and David have direct :knows relationships to Rahim. However, this does not tell the whole story.

 

Ram can reach Rahim through Hari and Krishna. Similarly, John can reach Rahim through Priya, Hari, and Krishna. To discover those indirect relationships, we need recursive paths.

 

2. Property Paths

A property path allows SPARQL to repeatedly follow a predicate. The two most commonly used recursive path operators are summarized in following table.

 

Operator

Meaning

*

Zero or more occurrences

+

One or more occurrences

 

2.1 Zero or More Occurrences (*)

The following query asks "Who can reach Rahim through zero or more :knows relationships?"

 

PREFIX : <http://example.org/social/>

SELECT ?personName
WHERE {
   ?person :knows* :Rahim ;
           :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
David
Hari
John
Krishna
Meera
Neha
Priya
Rahim
Ram
Suresh
Vikram

   

At first glance, one result may seem surprising.

 

Why is Rahim included?

The reason is that the * operator means zero or more occurrences. A path of length zero is valid.

 

Therefore, SPARQL considers: Rahim → Rahim without traversing any edge. This behavior is mathematically correct, but it is not always what users expect.

 

2.2 One or More Occurrences (+)

Most of the time, we want people who actually know Rahim either directly or indirectly. In that case, we should use the + operator.

 

PREFIX : <http://example.org/social/>

SELECT ?personName
WHERE {
   ?person :knows+ :Rahim ;
           :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
David
Hari
John
Krishna
Meera
Neha
Priya
Ram
Suresh
Vikram

   

Notice that Rahim no longer appears in the result set. The + operator requires at least one traversal of the :knows relationship. This is usually the preferred option when searching for friends, connections, dependencies, or lineage relationships.

 

2.3 Select all the persons that Hari knows

 

PREFIX : <http://example.org/social/>

SELECT DISTINCT ?personName
WHERE {
   ?Hari :knows+ ?person .
  
   ?person :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
Hari
John
Krishna
Priya
Rahim
Suresh
Vikram

   

2.4 Finding Everyone Connected to Hari

 

PREFIX : <http://example.org/social/>

SELECT DISTINCT ?personName
WHERE {
   ?person :knows+ ?Hari .
  
   ?person :name ?personName.
}
ORDER BY ?personName

   

Output

 

personName
Alex
Anita
David
Hari
John
Krishna
Meera
Neha
Priya
Ram
Suresh
Vikram

   

In summary, Property paths are one of the most powerful features of SPARQL. They allow us to navigate graph relationships without knowing the path length in advance.

 

Remember these two operators:

 

·      *: Zero or more occurrences

·      +: One or more occurrences

 

Use * when zero-length paths should be included. Use + when at least one relationship traversal is required.

 

Whenever you need to answer questions involving friends-of-friends, organizational hierarchies, dependencies, network connectivity, or data lineage, recursive property paths provide a concise and elegant solution.


Previous                                                    Next                                                    Home

SPARQL Property Paths: Chaining Relationships with Sequence Paths (/)

  

So far, we've seen how to traverse a single relationship in an RDF graph. However, many real-world queries require us to follow multiple relationships in sequence.

 

For example, in our Marvel dataset, teams can fight villains, and villains can appear in movies. Suppose we're not interested in the villain itself—we simply want to know which teams are connected to which movies through that chain of relationships.

 

This is where sequence paths come in. A sequence path allows us to follow one predicate and then immediately follow another, treating the entire chain as a single path.

 

In this post, we'll use SPARQL's sequence path operator (/) to navigate through multiple relationships while returning only the start and end nodes.

 

Let's begin with a problem. Suppose we want to find "Which teams are connected to which movies through villains they have fought?".

 

Looking at our data:

Avengers ──foughtAgainst──► Thanos ──appearsIn──► Infinity War
Avengers ──foughtAgainst──► Thanos ──appearsIn──► Avengers: Endgame

Guardians of the Galaxy ──foughtAgainst──► Thanos ──appearsIn──► Infinity War
Guardians of the Galaxy ──foughtAgainst──► Thanos ──appearsIn──► Avengers: Endgame

   

Notice that Thanos is the intermediate node. For this query, we don't really care about the villain in the middle. We simply want to know: Team ─────────► Movie.

 

while allowing SPARQL to traverse the intermediate relationships for us.

 

1. Using a Sequence Path

To accomplish this, we can use a sequence path.

PREFIX : <http://example.org/marvel/>

SELECT ?teamName ?movieTitle
WHERE {
    ?team :name ?teamName .

    ?team :foughtAgainst/:appearsIn ?movie .

    ?movie :title ?movieTitle .
}

   

Output

teamName

movieTitle

Avengers   

Avengers: Endgame

Avengers   

Avengers: Infinity War

Guardians of the Galaxy

Avengers: Endgame

 

Guardians of the Galaxy

Avengers: Infinity War

 

1.1 Understanding the Path

The expression ":foughtAgainst/:appearsIn" means:

 

·      Follow the :foughtAgainst relationship.

·      From there, follow the :appearsIn relationship.

·      Return the final node reached.

 

Visually:

 

Avengers
    │
    └── foughtAgainst
            │
            ▼
         Thanos
            │
            └── appearsIn
                    │
                    ▼
              Infinity War

The forward slash (/) tells SPARQL to traverse both relationships as a single path.

 

2. What Happens During Execution?

When SPARQL evaluates:

"?team :foughtAgainst/:appearsIn ?movie ."

 

it internally looks for patterns equivalent to:

?team :foughtAgainst ?villain .

?villain :appearsIn ?movie .

 

But without requiring us to explicitly introduce the intermediate variable ?villain. The sequence path hides the middle node and focuses only on the start and end nodes.

 

Notice that Thanos does not appear in the results. Even though SPARQL traverses through him internally, the sequence path only returns the start node (?team) and the end node (?movie).

 

Equivalent query without a Sequence Path

We could write the same query using an explicit intermediate variable:

PREFIX : <http://example.org/marvel/>

SELECT ?teamName ?movieTitle
WHERE {
    ?team :name ?teamName .
    ?team :foughtAgainst ?villain .

    ?villain :appearsIn ?movie .

    ?movie :title ?movieTitle .
}

This produces the same results. However, the sequence path version is often shorter and easier to read when the intermediate nodes are not important to the query.

 

3. Why Use Sequence Paths?

Sequence paths are useful when:

 

·      You want to follow multiple relationships in order.

·      Intermediate nodes are not important to the final result.

·      You want more concise and expressive SPARQL queries.

·      You're exploring indirect relationships in a graph.

 

As paths become longer and more complex, sequence paths can make queries significantly easier to understand.

 

In summary:

·      The forward slash (/) is the sequence path operator in SPARQL.

·      It allows multiple predicates to be traversed as a single path.

·      A sequence path returns only the start and end nodes, hiding any intermediate nodes.

·      The expression ":foughtAgainst/:appearsIn" means follow :foughtAgainst, then follow :appearsIn.

·      Sequence paths provide a concise alternative to writing multiple triple patterns with intermediate variables.

·      They are especially useful for discovering indirect relationships in RDF graphs.

 

 

Previous                                                    Next                                                    Home