Monday, 3 August 2026

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

No comments:

Post a Comment