Study Vault Search notes, aliases and full text
Graph
TopicDBISAI-written · not yet proofread

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#

  1. RDF Tripledata model: all data as triples, a set of triples is a directed labelled graph start here
  2. SPARQL Query StructureSELECT … WHERE { triples with variables }, PREFIX, FILTER, ORDER BY, LIMIT
  3. Reificationexpressing nn-ary facts with mediator entities (statement nodes)
  4. Wikidata Prefix Systemwd:, wdt:, p:, ps:, pq: and how to reach qualifiers
  5. SPARQL to SQL Translationone alias per triple, equalities for constants, m−1m-1 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 path p: → 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 nn-ary data.
TaskToolWatch out for
Question → SPARQLtriples with shared variablespattern should be connected; no commas in SELECT
SQL → SPARQLeach alias → one triple, equalities → shared variables!= becomes FILTER
SPARQL → SQLalias per triple, constants, m−1m-1 joinsone table vs. table per predicate
Qualifier needed (year, film, …)p: → ps: + pq:different variables for award and film; wdt: only on entities
Count WHERE conditionsfixed elements +∑v(mv−1)+ \sum_v (m_v - 1)count the predicate as a fixed element

Key Formulas#

  • Number of SQL WHERE conditions: #fixed elements+∑v(mv−1)\#\text{fixed elements} + \sum_v (m_v - 1) (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#

SemesterTaskPointsSubtopics
DBIS Exam WS1314 (not published)T620SPARQL query + SQL equivalent for actor/director ontology
DBIS Exam WS1516 (not published)T65SPARQL for actors with European directors
DBIS Exam WS1819 (not published)T610SPARQL for female astronauts + equivalent SQL, POS-Tagging
DBIS Exam WS1920 (not published)T510SPARQL-to-SQL conditions function, Viterbi END tag
DBIS Exam WS2122 (not published)T35SPARQL for Olympians from German-speaking countries
DBIS Exam WS2223 (not published)T55SPARQL for YouTubers at same university as MrBeast
DBIS Exam WS2324 (not published)T320SQL-to-SPARQL translation, Wikidata prefix evaluation, NATURAL JOIN
DBIS Exam WS2324-2 (not published)T320SQL-to-SPARQL (same mother), reification of two terms, join with subquery

Lectures#

  • DBIS_VL07 — RDF, SPARQL, reification and Wikidata prefixes, SPARQL to SQL translation

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only