SPARQL/WIKIDATA Qualifiers, References and Ranks

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Expressions and Functions SPARQL
WIKIDATA Qualifiers, References and Ranks
WIKIDATA Language links and Badges
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]

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]

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]

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!

Summary[edit]

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


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]


Expressions and Functions SPARQL
WIKIDATA Qualifiers, References and Ranks
WIKIDATA Language links and Badges