SPARQL query

This example story is part of the SHMARQL documentation. It demonstrates the SPARQL Special Node. To open it as an editable project, download sparql-query.lodeon.zip and import it in the story editor (/lodeon/ → Import).

A SPARQL node holds a query and the knowledge graph (a SHMARQL profile) it runs against. In the editor you run it with Run and get a paged, filterable result table. On the published page the reader's browser runs the same query again, so the table is always up to date.

A query written by hand

All paintings by Johannes Vermeer (Q41264), with the year they were made and where they are now:

SELECT ?painting ?paintingLabel ?year ?locationLabel WHERE {
  ?painting wdt:P170 wd:Q41264 ;
            wdt:P31 wd:Q3305213 .
  OPTIONAL { ?painting wdt:P571 ?date . BIND(YEAR(?date) AS ?year) }
  OPTIONAL { ?painting wdt:P276 ?location . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?year
LIMIT 100

Links in the result table open the entity viewer of SHMARQL, which shows any Wikidata item as a readable page.

A query from the Visual Query Builder

When a profile has a Sparnatural configuration (sparnatural: in the profile YAML), the SPARQL node also offers the Visual Query Builder. You click a query together – for example Painting → painted by → Painter – and the builder writes the SPARQL. This node was made that way, and narrowed down by hand to Vermeer's paintings from the 1660s:

SELECT DISTINCT ?Painting_1 ?Painting_1_label WHERE {
  ?Painting_1 wdt:P31 wd:Q3305213 .
  ?Painting_1 wdt:P170 wd:Q41264 .
  ?Painting_1 wdt:P571 ?Date_2 .
  FILTER(YEAR(?Date_2) >= 1660 && YEAR(?Date_2) < 1670)
  ?Painting_1 rdfs:label ?Painting_1_label .
  FILTER(LANG(?Painting_1_label) = "en")
}
LIMIT 100

Try it in the editor:

  • Change LIMIT 100 to LIMIT 5 and select Run.
  • Replace wd:Q41264 (Vermeer) with wd:Q5598 (Rembrandt).
  • Open the Visual Query Builder on the second node and add a criterion.
  • Use the knowledge-graph selector in the top bar: new nodes use the selected profile.