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 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 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).
# Prefixes declaration
PREFIX foo: <http://example.com/resources/>
...
# Graph definition
FROM ...
# Results settings
SELECT ...
# Query
WHERE {
...
}
# Modifiers
ORDER BY ...
- DBpedia (Site project, SPARQL endpoint).
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label
WHERE {
<http://dbpedia.org/resource/Stanley_Kubrick> rdfs:label ?label
}
- Get all properties and all objects related to the http://dbpedia.org/resource/Stanley_Kubrick resource.
SELECT ?movie ?distributor
WHERE {
?movie <http://dbpedia.org/ontology/director> <http://dbpedia.org/resource/Stanley_Kubrick> .
?movie <http://dbpedia.org/ontology/distributor> ?distributor .
}
- Get founders and founding dates of all distribution houses of movies directed by Kubrick.
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
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
- Logical: !, &&, ||
- Math: +, -, *, /
- Comparison: =, !=, >, <, ...
- SPARQL tests: isURI, isBlank, isLiteral, bound
- SPARQL accessors: str, lang, datatype
- Other: sameTerm, langMatches, regex
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")) .
}
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")) .
}
- Get all directors in DBpedia and all movies in which these directors had the role of producers.
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> .
}
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 .
}
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) )
}
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 .
}
}
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
}
An Italian version of this course is available at: https://github.com/giuseppefutia/sparql-course