AD Research Wiki:

Wikidata: various explanations

A good SPARQL tutorial specifically for Wikidata and its particular schema can be found here: https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial

One thing that is particularly tricks is to obtain names for entities and predicates. The following section explains how that works.

Obtaining names for entities and predicates

The Wikidata Query Service (WDQS) has an automatic mechanism for obtaining the single best name for entity variables in the query. It does not work for predicate names, however. The mechanism is invoked by adding this line to the SPARQL query:

SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }

To obtain a single name of an entity "manually" (= without the mechanism above), one can add the following two lines to a query (Q90 is Paris). Note that without the language filter, one gets one name for each languages, for which a name has been specified for that entity in Wikidata (for popular entities, these are usually very many):

wd:Q90 rdfs:label ?label .
FILTER(LANG(?label) = "en") 

Obtaining the name for a predicate is a bit more complicated. Here is how to obtain all predicates and their English name for an entity (again Q90), sorted by the number of triples with that predicate and that entity as subject:

SELECT ?p (COUNT(?o) as ?count) WHERE {
  wd:Q90 ?p ?o .
  ?x wikibase:claim ?p .
  ?x rdfs:label ?label .
  FILTER(LANG(?label) = "en")
}
GROUP BY ?p
ORDER BY DESC(?count)
LIMIT 100

There are a number of other wikibase: predicates that relate the different types of predicates to teach other

wd:P47 wikibase:claim              p:P47
wd:P47 wikibase:directClaim        wdt:P47
wd:P47 wikibase:novalue            wdno:P47
wd:P47 wikibase:qualifier          pq:P47
wd:P47 wikibase:qualifierValue     pqv:P47
wd:P47 wikibase:reference          pr:P47
wd:P47 wikibase:referenceValue     prv:P47
wd:P47 wikibase:statementProperty  ps:P47
wd:P47 wikibase:statementValue     psv:P47

AD Research Wiki: HowTos/WikidataCheatSheet (last edited 2018-04-21 21:43:24 by Hannah Bast)