/big-dive-sparql

Crash course on the SPARQL query language

Primary LanguageHTMLApache License 2.0Apache-2.0

SPARQL crash course

Crash course on the SPARQL query language within the Big Dive training program.

Contents published on this repository are based on the W3C Candidate Recommendation “SPARQL Query Language for RDF” from http://www.w3.org/TR/rdf-sparql-query/.

SPARQL Architecture

  • SPARQL queries are executed against RDF datasets, consisting of RDF graphs.
  • A SPARQL endpoint accepts queries and returns results via HTTP.
  • The results of SPARQL queries can be returned and/or rendered in a variety of formats:
    • XML. SPARQL specifies an XML vocabulary for returning tables of results.
    • JSON. A JSON "port" of the XML vocabulary, particularly useful for Web applications.
    • RDF. Certain SPARQL result clauses trigger RDF responses, which in turn can be serialized in a number of ways (RDF/XML, N-Triples, Turtle, etc.)
    • HTML. When using an interactive form to work with SPARQL queries. Often implemented by applying an XSL transform to XML results.

SPARQL basics

  • SPARQL variables start with a ? and can match any node (resource or literal) in the RDF dataset.
  • Triple patterns are just like triples, except that any of the parts of a triple can be replaced with a variable (pattern matching).
  • Variables named after the SELECT keyword are the variables that will be returned as results (~SQL).

SPARQL query basic structure

# Prefixes declaration
PREFIX foo: <http://example.com/resources/>
...
# Graph definition
FROM ...
# Results settings
SELECT ...
# Query
WHERE {
    ...
}
# Modifiers
ORDER BY ...

Endpoint used for this crash course

Running our first query

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?label
WHERE {
   <http://dbpedia.org/resource/Stanley_Kubrick> rdfs:label ?label
}

Perform the query

Excercise

Multiple triple patterns and graph traversal

SELECT ?movie ?distributor
WHERE {
    ?movie <http://dbpedia.org/ontology/director> <http://dbpedia.org/resource/Stanley_Kubrick> .
    ?movie <http://dbpedia.org/ontology/distributor> ?distributor .
}

Perform the query

Excercise

  • Get founders and founding dates of all distribution houses of movies directed by Kubrick.

SPARQL modifiers

The objective of modifiers is slicing, ordering, and otherwise rearranging query results.

  • DISTINCT
  • LIMIT
  • OFFSET
  • ORDER BY
SELECT DISTINCT ?director ?directorLabel
WHERE {
    ?movie <http://dbpedia.org/ontology/director> ?director .
    ?director rdfs:label ?directorLabel .
} ORDER BY ASC(?directorLabel) LIMIT 50 OFFSET 200

Perform the query

SPARQL filters

FILTER constraints use boolean conditions to filter out unwanted query results.

SELECT DISTINCT ?director ?directorLabel
WHERE {
    ?movie <http://dbpedia.org/ontology/director> ?director .
    ?director rdfs:label ?directorLabel .
    FILTER (langMatches(lang(?directorLabel), "IT")) .
} LIMIT 50

Perform the query

Built-in filters

  • Logical: !, &&, ||
  • Math: +, -, *, /
  • Comparison: =, !=, >, <, ...
  • SPARQL tests: isURI, isBlank, isLiteral, bound
  • SPARQL accessors: str, lang, datatype
  • Other: sameTerm, langMatches, regex

OPTIONAL pattern

OPTIONAL tries to match a graph pattern, but doesn't fail the whole query if the optional match fails.

SELECT DISTINCT ?director ?directorLabel ?quote
WHERE {
    ?movie <http://dbpedia.org/ontology/director> ?director .
    ?director rdfs:label ?directorLabel .
    ?director <http://dbpedia.org/property/quote> ?quote . 
    FILTER (langMatches(lang(?directorLabel), "EN")) .
}

Perform the query

SELECT DISTINCT ?director ?directorLabel ?quote
WHERE {
    ?movie <http://dbpedia.org/ontology/director> ?director .
    ?director rdfs:label ?directorLabel .
    OPTIONAL {?director <http://dbpedia.org/property/quote> ?quote} . 
    FILTER (langMatches(lang(?directorLabel), "EN")) .
}

Perform the query

Exercise

  • Get all directors in DBpedia and all movies in which these directors had the role of producers.

DESCRIBE and CONSTRUCT

In addition to the SELECT clause, you can specify other keywords depending on the result we want to achieve with our queries.

DESCRIBE ?movie {
   ?movie <http://dbpedia.org/ontology/director> <http://dbpedia.org/resource/Stanley_Kubrick> .
}

Perform the query

PREFIX mo: <http://myontology.org/>

CONSTRUCT { 
  <http://dbpedia.org/resource/Stanley_Kubrick> mo:workWithDistributor ?distributor .
}
WHERE { 
    ?movie <http://dbpedia.org/ontology/director> <http://dbpedia.org/resource/Stanley_Kubrick> .
    ?movie <http://dbpedia.org/property/distributor> ?distributor .
}

Perform the query

Advanced SPARQL technique: Negation

SELECT DISTINCT ?movie
WHERE {
    ?movie <http://dbpedia.org/ontology/director> <http://dbpedia.org/resource/Stanley_Kubrick> .
    ?movie <http://dbpedia.org/property/distributor> ?distributor .
    OPTIONAL {<http://dbpedia.org/resource/Blade_Runner> <http://dbpedia.org/property/distributor> ?badDistributor . FILTER (?distributor = ?badDistributor) .} .
    FILTER ( !BOUND(?badDistributor) )
}

Perform the query

Advanced SPARQL technique: Sum

PREFIX pc: <http://purl.org/procurement/public-contracts#>
PREFIX payment: <http://reference.data.gov.uk/def/payment#>

SELECT SUM(?amount) as ?paidTotal ?company
WHERE {
    SELECT DISTINCT ?contract ?amount ?company
    WHERE {
        ?company <http://purl.org/goodrelations/v1#vatID> "04145300010".
        ?bid pc:bidder ?company .
        ?contract pc:awardedTender ?bid .
        ?contract payment:payment ?payment . 
        ?payment payment:netAmount ?amount .
    }
}

Perform the query

Federated SPARQL query

PREFIX gr:<http://purl.org/goodrelations/v1#>
PREFIX pc: <http://purl.org/procurement/public-contracts#>
PREFIX payment: <http://reference.data.gov.uk/def/payment#>
PREFIX spc: <http://spcdata.digitpa.gov.it/>

SELECT ?label SUM(?amount) as ?paidAmounts ?officialEmail
WHERE {
   SELECT DISTINCT *
   WHERE {
      ?contractingAutority <http://purl.org/goodrelations/v1#vatID> "00518460019".
      ?contractingAutority rdfs:label ?label.
      ?contractingAutority owl:sameAs ?uriSpc.
      SERVICE <http://spcdata.digitpa.gov.it:8899/sparql> { 
         OPTIONAL { ?uriSpc spc:PEC ?officialEmail.} 
         }
         ?contract pc:contractingAutority  ?contractingAutority.
         ?contract payment:payment ?payment.
         ?payment payment:netAmount ?amount.
      } ORDER BY ?contract
   } 

Perform the query

An Italian version of this course is available at: https://github.com/giuseppefutia/sparql-course