SPARQL Query Structure
A basic SPARQL query `SELECT <variables> WHERE { <triples> }` is a graph pattern of triples with variables; its result is a table with one row per assignment of values to the variables such that all query triples exist in the knowledge graph.
Properties#
- Graph pattern. The query triples form a small graph with variables at some nodes or edges. Answering the query means finding all matches of this pattern in the large KG; each distinct match is one assignment (one row).
- Joins are implicit. Using the same variable in several triples is the join; there is no explicit join condition.
Syntax elements:
| Element | Syntax | Meaning |
|---|---|---|
| variable | ?name | bound to values during matching |
| fixed entity / predicate | <Entity>, or prefixed wd:Q42, wdt:P31 | a known IRI |
| literal | "1980", "Honolulu"@en | a fixed value |
| query triple | s p o . | one constraint; triples are separated by . (the last . before } is optional) |
SELECT * | selects all variables |
Modifiers (as in SQL):
PREFIX wd: <http://www.wikidata.org/entity/>abbreviates long IRIs.FILTER (condition)keeps only rows satisfying the condition, e.g.FILTER (LANG(?name) = "en")for English labels,FILTER (?a != ?b).ORDER BY DESC(?x)/ASC(?x),LIMIT k; note thatDESC/ASCis written like a function of the sort key.
Differences to SQL:
| SPARQL | SQL | |
|---|---|---|
| data | one big “table” of triples (but: named graphs) | many tables, FROM clause |
| main instrument | triples with variables | FROM + WHERE conditions |
| join | same variable in several triples | explicit equality conditions (Join) |
SELECT list | no commas: SELECT ?a ?b | commas: SELECT a, b |
| sort order | DESC(?var) | var DESC |
Expressiveness. Basic SPARQL and SQL are essentially equivalent; basic queries translate mechanically (SPARQL to SQL Translation).
Example#
All people born in Honolulu with their English names and birth dates, youngest first:
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?person ?person_name ?date_of_birth WHERE {
?person wdt:P19 wd:Q18094 . # place of birth: Honolulu
?person wdt:P569 ?date_of_birth . # date of birth
?person rdfs:label ?person_name .
FILTER (LANG(?person_name) = "en")
}
ORDER BY DESC(?date_of_birth) LIMIT 100
Married couples who acted in the same film (?film and ?person1/?person2 are the shared variables, i.e. the joins):
SELECT ?person1 ?person2 ?film WHERE {
?person1 <acted_in> ?film .
?person2 <acted_in> ?film .
?person1 <married_to> ?person2 .
}
Simplest case: SELECT ?person ?occ WHERE { ?person <born_in> <Ulm> . ?person <occupation> ?occ . } returns each person born in Ulm with each of their occupations.
Sources#
- Slides 07, p. 12 (SPARQL), p. 13 (basic queries), p. 14 (graph patterns), pp. 15–16 (PREFIX, FILTER, ORDER BY, LIMIT), p. 17 (SPARQL vs. SQL)
- DBIS_VL07 > SPARQL, DBIS_VL07 > Basic queries, DBIS_VL07 > Queries as graph patterns, DBIS_VL07 > PREFIX, FILTER, ORDER BY, LIMIT, DBIS_VL07 > Differences between SPARQL and SQL
- Exams: DBIS Exam WS1819 (not published) (Task 6.1), DBIS Exam WS2122 (not published) (Task 3.1), DBIS Exam WS2223 (not published) (Task 5), DBIS Exam WS2324 (not published) (Task 3.1)