SPARQL/WIKIDATA Qualifiers, References and Ranks
The data on WIKIDATA contains more info than only the triples. For a full description see Wikidata:Glossary.
Let us look at Douglas Adams (Q42) and where he is educated at (P69).
Qualifiers
[edit | edit source]Let us list the education of Douglas Adams and the qualifiers Start time end End time:
SELECT ?education ?educationLabel ?starttime ?endtime
WHERE
{
wd:Q42 p:P69 ?statement.
?statement ps:P69 ?education.
?statement pq:P580 ?starttime.
?statement pq:P582 ?endtime.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?starttime
The prefix p:
points not to the object, but to a statement node. This node then is the subject of other triples.
The prefix ps:
within the statement node retrieves the object.
The prefix pq:
within the statement node retrieves the qualifier information.
The code can be abbreviated a lot with the [ ] syntax by eliminating the variable ?statement
.
SELECT ?education ?educationLabel ?starttime ?endtime
WHERE
{
wd:Q42 p:P69 [ps:P69 ?education;
pq:P580 ?starttime;
pq:P582 ?endtime;
].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?starttime
References
[edit | edit source]Let us list the education of Douglas Adams and the stated in (P248) reference:
SELECT ?education ?educationLabel ?ref ?refLabel
WHERE
{
wd:Q42 p:P69 ?statement.
?statement ps:P69 ?education.
?statement prov:wasDerivedFrom ?refnode.
?refnode pr:P248 ?ref.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
The prefix p:
points not to the object, but to a statement node. This node then is the subject of other triples.
The prefix ps:
within the statement node retrieves the object.
The prov:wasDerivedFrom
within the statement node points to a new reference node.
The prefix pr:
within the reference node retrieves the reference information.
The code can be abbreviated a lot with the [ ] syntax by eliminating the variables ?statement
and ?refnode
.
SELECT ?education ?educationLabel ?ref ?refLabel
WHERE
{
wd:Q42 p:P69 [ ps:P69 ?education;
prov:wasDerivedFrom
[ pr:P248 ?ref;
]
].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
You might have noticed that only one of the 2 educations is listed in the queries above. To list both we need to introduce OPTIONAL{ }
. As this can only be used with full sentences we need to use the full expanded syntax with triples as short sentence:
SELECT ?education ?educationLabel ?ref ?refLabel
WHERE
{
wd:Q42 p:P69 ?statement.
?statement ps:P69 ?education.
OPTIONAL{ ?statement prov:wasDerivedFrom ?refnode.
?refnode pr:P248 ?ref.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Ranks
[edit | edit source]Let us list the education of Douglas Adams and the ranks of them:
SELECT ?education ?educationLabel ?rank
WHERE
{
wd:Q42 p:P69 ?statement.
?statement ps:P69 ?education.
?statement wikibase:rank ?rank.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
The prefix p:
points not to the object, but to a statement node. This node then is the subject of other triples.
The prefix ps:
within the statement node retrieves the object.
The wikibase:rank
within the statement node retrieves the rank information.
The code can be abbreviated a lot with the [ ] syntax by eliminating the variable ?statement
.
SELECT ?education ?educationLabel ?rank
WHERE
{
wd:Q42 p:P69 [ps:P69 ?education;
wikibase:rank ?rank;
].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
An example with different ranks is the (historcal) countries in which Berlin lied.
# Berlins countries and ranking
SELECT ?country ?countryLabel ?rank
WHERE
{
wd:Q64 p:P17 [ps:P17 ?country;
wikibase:rank ?rank;
].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
The 3 possible values for ranks are wikibase:PreferredRank
, wikibase:NormalRank
and wikibase:DeprecatedRank
Compare this with the normal triples, which will select only the value(s) with the highest rank. In this case only the Preferred Rank value Germany (Q183).
# Berlins countries via normal triples
SELECT ?country ?countryLabel
WHERE
{
wd:Q64 wdt:P17 ?country.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Ignore ranks in queries
[edit | edit source]Ranks might cause unexpected results. For example, consider this query, that will give you back all Dutch municipalities that share a border with Alphen aan den Rijn (Q213246):
select ?muni ?muniLabel where {
?muni wdt:P31 wd:Q2039348;
wdt:P47 wd:Q213246.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
This will not show Boskoop (Q894442), because its rank for municipality of the Netherlands (Q2039348) is 'normal' while the other three values for instance of (P31) are 'preferred'. To see Boskoop in the previous query as well, rewrite it like this:
select ?muni ?muniLabel where {
?muni p:P31 [ps:P31 wd:Q2039348];
wdt:P47 wd:Q213246.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Or use a property path to shorten the query like this:
select ?muni ?muniLabel where {
?muni p:P31/ps:P31 wd:Q2039348;
wdt:P47 wd:Q213246.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Summary
[edit | edit source]Example | ||
---|---|---|
Statements | wd:Q42 wdt:P69 wd:Q691283. or wd:Q42 p:P69 ?s. ?s ps:P69 wd:Q691283. or wd:Q42 p:P69 [ ps:P69 wd:Q691283 ]. |
|
Rank | wd:Q42 p:P69 [ wikibase:rank ?rank ]. | |
Qualifier | wd:Q42 p:P69 [ pq:P580 ?qualifier ]. | |
Reference | wd:Q42 p:P69 [ prov:wasDerivedFrom [ pr:P248 ?ref ] ]. |
A full query of Douglas Adams education, with rank, qualifiers and references could look like
# Douglas Adams education, with rank, qualifiers and references
SELECT ?education ?educationLabel ?rank ?starttime ?endtime ?ref ?refLabel
WHERE
{
wd:Q42 p:P69 ?statement.
?statement ps:P69 ?education.
# rank
?statement wikibase:rank ?rank.
# qualifiers
OPTIONAL{ ?statement pq:P580 ?starttime. }
OPTIONAL{ ?statement pq:P582 ?endtime. }
# references
OPTIONAL{ ?statement prov:wasDerivedFrom ?refnode.
?refnode pr:P248 ?ref.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?starttime
References
[edit | edit source]