Search code examples
databasesemanticssemantic-websparql

Sparql without FROM keyword


Glad if someone can clarify.

I always read that SQL and SPARQL are very similar. However, a lot of times i have seen sparql query without the FROM keyword. You would assume that you have to query the datasource 'FROM' somewhere in the database like how SQL behaves.

Example

PREFIX foaf:  <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE 
{
    ?person foaf:name ?name .
}

How does it know where to look for? Like in SQL, look in Database1 - table1

Cheers


Solution

  • SPARQL queries are execute against an RDF dataset, which has two parts:

    • A single default graph: a set of triples with no name attached to them
    • Zero or more named graphs: a set of triples identified by a name

    You can specify the graph to query using the optional FROM or FROM NAMED clauses. Have a look at Specifying RDF Datasets. If you omit those clauses, then the query is executed on the default graph.


    Graphs and RDF dataset storage is implementation dependent. For example, using Jena ARQ you use a QueryExecutionFactory and you can initialize it in two ways:

    • passing a Model, which becomes the default graph on which queries are executed
    • passing a Dataset, which can contain a single default graph and several named graphs

    Code Examples for Jena ARQ are available here. Other implementation may be different.