Your First SPARQL Query Steps

Consider the following set of triples:

subject predicate object
countries:Germany rdf:type countries:Country
countries:Switzerland rdf:type countries:Country
countries:UnitedStates rdf:type countries:Country
countries:UnitedStates rdfs:label "United States"
countries:TheNetherlands rdfs:label "The Netherlands"

 


Select all triples

SPARQL Queries are build of statements and triples. When you look at the following query you see a SELECT statement.

SELECT ?subject ?predicate ?object
WHERE {
    ?subject ?predicate ?object
}

With this statement the result is a set of all existing triples. Within the WHERE clause you can restrict the resultset by replacing the statement ?subject, ?predicate or ?object. The last query has the following result:

subject predicate object
countries:Germany rdf:type countries:Country
countries:Switzerland rdf:type countries:Country
countries:UnitedStates rdf:type countries:Country
countries:UnitedStates rdfs:label "United States"
countries:TheNetherlands rdfs:label "The Netherlands"

  


Select the triples with label  "United States"

When we want to select just the triples with the label "United States" we have to set the predicate and the object in the WHERE clause. The predicate is "rdfs:label" and the subject will be the String "United States".

SELECT ?subject ?predicate ?object
WHERE {
    ?subject rdfs:label "United States"
}

This query deliveres the following resultset:

subject predicate object
countries:UnitedStates rdfs:label "United States"

 


Select the subjects with object countries:Country

To only select the subject part of the triples that have an object "countries:Country" use the following query:

SELECT ?subject
WHERE {
    ?subject ?predicate countries:Country
}

With this statement the result is a set of all subjects out of a triple with subject countries:Country.

subject
countries:Germany
countries:Switzerland
countries:UnitedStates

 

Leave a comment

You must be logged in to post a comment.