Jump to content

SPARQL/WIKIDATA Qualifiers, References and Ranks

From Wikibooks, open books for an open world
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Wikidata datamodel

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

Try it!

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

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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". }
}

Try it!

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 ].
Wikidata datamodel
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 ] ].
SPARQL data representation, as used by Wikidata Query Service Wikibase RDF mapping diagram

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

Try it!

References

[edit | edit source]