Skip to content

How to query a statement with Qualifiers and References in Wikidata

Junjun Cao edited this page Aug 21, 2024 · 4 revisions

Let’s use a well-known item with qualifiers and references to ensure we get results. We’ll query for the population of Germany (Q183) with qualifiers for the point in time and the reference URL.

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX pr: <http://www.wikidata.org/prop/reference/>
PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT ?population ?pointInTime ?referenceUrl
WHERE {
  wd:Q183 p:P1082 ?statement .       # Germany (Q183) and its population statement
  ?statement ps:P1082 ?population .  # The population value
  OPTIONAL { ?statement pq:P585 ?pointInTime . }  # The point in time as a qualifier
  OPTIONAL {
    ?statement prov:wasDerivedFrom ?referenceNode .
    ?referenceNode pr:P854 ?referenceUrl .  # The reference URL
  }
}
LIMIT 10

Explanation

1. PREFIX: Defines common prefixes used in the query.

wd: for Wikidata entities.
wdt: for direct properties in Wikidata.
p: for properties in the context of statements.
ps: for the main value of a statement.
pq: for qualifiers.
pr: for references.
prov: for provenance information.

2. SELECT ?population ?pointInTime ?referenceUrl: Specifies the variables to retrieve.

?population: The population value.
?pointInTime: The point in time as a qualifier.
?referenceUrl: The reference URL.

3. WHERE: Specifies the pattern to match in the data.

wd:Q183 p:P1082 ?statement .: Finds the statement about the population of Germany (Q183).
?statement ps:P1082 ?population .: Retrieves the population value from the statement.
OPTIONAL { ?statement pq:P585 ?pointInTime . }: Optionally retrieves the point in time as a qualifier.
OPTIONAL { ?statement prov:wasDerivedFrom ?referenceNode . ?referenceNode pr:P854 ?referenceUrl . }: Optionally retrieves the reference URL.

The Query Result

image