SPARQL and Knowledge Graphs
RDF triples as the data model of knowledge graphs, SPARQL as their query language, Wikidata's statement-node scheme for n-ary facts, and the mechanical translation of basic SPARQL to SQL.
Concepts#
- RDF Tripledata model: all data as triples, a set of triples is a directed labelled graph start here
-
SPARQL Query Structure
SELECT … WHERE { triples with variables }, PREFIX, FILTER, ORDER BY, LIMIT - Reificationexpressing -ary facts with mediator entities (statement nodes)
-
Wikidata Prefix System
wd:,wdt:,p:,ps:,pq:and how to reach qualifiers - SPARQL to SQL Translationone alias per triple, equalities for constants, joins per variable
How it fits together#
The chain is: data model → query language → hard case → target relation.
- Triples → patterns. A KG is a set of triples; a SPARQL query is a small graph of triples with variables, and its result table lists all matches in the KG. A variable shared by two triples is a join.
- Binary only → reification. Triples are binary, so a fact like “award, year, film” needs a mediator (Reification). Wikidata does this with statement nodes and offers two views of each property: the shortcut
wdt:(no mediator, no qualifiers) and the full pathp:→ps:/pq:(Wikidata Prefix System). - Patterns → SQL. Every basic query is a join of copies of the triples table (SPARQL to SQL Translation), so SPARQL and SQL are essentially equally expressive. The difference is convenience: SPARQL needs no schema and combines datasets with a few linking triples; SQL is easier for -ary data.
| Task | Tool | Watch out for |
|---|---|---|
| Question → SPARQL | triples with shared variables | pattern should be connected; no commas in SELECT |
| SQL → SPARQL | each alias → one triple, equalities → shared variables | != becomes FILTER |
| SPARQL → SQL | alias per triple, constants, joins | one table vs. table per predicate |
| Qualifier needed (year, film, …) | p: → ps: + pq: | different variables for award and film; wdt: only on entities |
Count WHERE conditions | fixed elements | count the predicate as a fixed element |
Key Formulas#
- Number of SQL
WHEREconditions: (SPARQL to SQL Translation).
Exercises & Exam Questions#
SuccessSolution
SELECT ?mother ?father WHERE {
?child has_mother ?mother .
?child has_father ?father .
?mother married_to ?father .
}
It returns all pairs of mother and father who have a child together and are married to each other.
SuccessSolution
Query 1 (wdt: throughout): False. wdt:P166 gives the award entity directly, but ?a wdt:P1686 ?b looks for “for work” on the award itself. Qualifiers are on statement nodes, not on the direct value.
Query 2 (p:/ps:/pq:, same variable ?b for award and film): False. ?a ps:P166 ?b binds ?b to the award, but ?a pq:P1686 ?b requires the film to be that same entity. Also, ?a wdt:P31 does not work on a statement node.
Query 3 (p:/ps:/pq:, distinct ?b for award and ?c for film): True. It navigates the statement node correctly and uses ?b wdt:P31 wd:Q19020 to filter for Oscars.
SuccessSolution
SELECT ?p ?b WHERE {
<MrBeast> <educated_at> ?u .
?p <educated_at> ?u .
?p <occupation> <YouTuber> .
?p <place_of_birth> ?b .
}
SuccessSolution
One statement node per term; see the worked example in Reification: wd:Q35171 p:P39 wds:Q35171-1 . wds:Q35171-1 ps:P39 wd:Q11696 . wds:Q35171-1 pq:P580 "1885" . and the same with -2 and "1893".
Exam occurrences#
| Semester | Task | Points | Subtopics |
|---|---|---|---|
| DBIS Exam WS1314 (not published) | T6 | 20 | SPARQL query + SQL equivalent for actor/director ontology |
| DBIS Exam WS1516 (not published) | T6 | 5 | SPARQL for actors with European directors |
| DBIS Exam WS1819 (not published) | T6 | 10 | SPARQL for female astronauts + equivalent SQL, POS-Tagging |
| DBIS Exam WS1920 (not published) | T5 | 10 | SPARQL-to-SQL conditions function, Viterbi END tag |
| DBIS Exam WS2122 (not published) | T3 | 5 | SPARQL for Olympians from German-speaking countries |
| DBIS Exam WS2223 (not published) | T5 | 5 | SPARQL for YouTubers at same university as MrBeast |
| DBIS Exam WS2324 (not published) | T3 | 20 | SQL-to-SPARQL translation, Wikidata prefix evaluation, NATURAL JOIN |
| DBIS Exam WS2324-2 (not published) | T3 | 20 | SQL-to-SPARQL (same mother), reification of two terms, join with subquery |
Lectures#
- DBIS_VL07 — RDF, SPARQL, reification and Wikidata prefixes, SPARQL to SQL translation
Related Topics#
- Fuzzy Search and Q-Grams — autocompletion of entity names (as in the QLever UI) motivates fuzzy prefix search; the dictionary of ES8 is a list of Wikidata entities (Slides 07, p. 26, Slides 08, p. 5)
- SQL and Query Planning — the target language of the translation
- Web Applications — the web app of ES9/ES10 shows knowledge-graph triples for each fuzzy-matched entity, queried through the SPARQL-to-SQL engine of ES7