Skip to main content

Named constructs

SPARQL does have CONSTRUCT queries which return graphs, but it has no way of using those as inputs to another query.  data.world has the named contructs extension to SPARQL to remedy this lack.  They are named by virtue of having an IRI. We have created two language constructs to allow you to improve your SPARQL experience:

  • WITH

  • Query-as-graph

Important

All the links in the following examples open datasets/projects in the data.world open data community. For best experience, make sure you are logged in to the application when you click these links.

WITH

Similar to WITH in SQL, named constructs allow you to specify a CONSTRUCT query as a prelude to other queries and to bind the results of that CONSTRUCT to an IRI.  That IRI can then be used in the subsequent queries as a named graph. In this way SPARQL queries can be composed, with the results of one query being inputs to another.  During execution, the first query is executed, and then its results can be used in the subsequent query. Multiple queries can be chained together in a WITH, and subsequent queries in the WITH can reference earlier ones.

Example

Here is a query showing data.world's WITH extension to SPARQL, showing chaining of SPARQL queries as named constructs:

PREFIX : <https://ddw-doccorp.linked.data.world/d/sparql-with/>
PREFIX airports: <https://airports.com/>

WITH GRAPH <https://airport-cities> {
    CONSTRUCT {
        ?s airports:city  ?city.
        ?s airports:state ?state.
    }
    WHERE {
        ?s a                     airports:airport.
        ?s airports:abbreviation ?abbreviation.
        ?q :col-cities-airport   ?abbreviation.
        ?q :col-cities-city      ?city.
        ?q :col-cities-state     ?state.
    }
}
SELECT ?city ?state
FROM <https://airport-cities>
{
    ?airport <https://airports.com/city>  ?city.
    ?airport <https://airports.com/state> ?state.
}

Run query

This is what it looks like when run on data.world:

WITH.png

Query-as-graph

Every query in data.world has an IRI associated with it. For SPARQL CONSTRUCT queries, those IRIs can be used in other queries as named graphs.  To find the IRI of a CONSTRUCT query:

  1. Select the three-dot menu to the right of the CONSTRUCT query name in the dataset or project workspace.

  2. Choose Copy Graph IRI

  3. Paste the results into the query as in the Query-as-graph example below.

CONSTRUCT_graph_IRI.png

Note

This is the query IRI referenced above: //ddw-doccorp.linked.data.world/d/sparql-with/q/7cff3f29-4304-46ab-973e-f10716f60829. When you want to refer to a query through the API you will need the query id, which is the last part of the query IRI: 7cff3f29-4304-46ab-973e-f10716f60829.

Similar to the WITH functionality, this IRI allows the results of one query to be used as inputs to another query. During execution, the referenced query is executed first, and then its results are used in the subsequent query. No caching or snapshotting of query results is done.  The referenced query is effectively a “live” graph, recreated fresh with each execution of the referencing query.

Unlike WITH, this query-as-graph functionality allows chained queries to be managed independently.  This allows referenced queries to be shared between multiple referencing queries.

Example - CONSTRUCT

Here is an example of a SPARQL CONSTRUCT query, which will be referenced by another query using query-as-graph functionality:

PREFIX : <https://ddw-doccorp.linked.data.world/d/sparql-with/>
PREFIX airports: <https://airports.com/>
CONSTRUCT {
    ?s airports:city  ?city.
    ?s airports:state ?state.
    }
WHERE {
    ?s a                     airports:airport.
    ?s airports:abbreviation ?abbreviation.
    ?q :col-cities-airport   ?abbreviation.
    ?q :col-cities-city      ?city.
    ?q :col-cities-state     ?state.
}

Run query

And this is what it looks like on data.world:

CONSTRUCT.png

Example - Query-as-graph

Here is a query showing data.world's query-as-graph functionality, referencing another query as a named graph:

PREFIX : <https://ddw-doccorp.linked.data.world/d/sparql-with/>
PREFIX q: <https://ddw-doccorp.linked.data.world/d/sparql-with/q/>

SELECT ?city ?state
FROM q:7cff3f29-4304-46ab-973e-f10716f60829
{
    ?airport <https://airports.com/city>  ?city.
    ?airport <https://airports.com/state> ?state.
}

Run query

And here is what it looks like on data.world:

query-as-graph.png